1

I wrote the following code to delete a file from the SD Card:

/**
 * Deletes a file
 * 
 * @param pathToFile
 *            Path to file, eg "/sdcard/test.txt"
 * @throws IOException
 *             Throws if file doesnt exist
 */
public static void deleteFile(String pathToFile) throws IOException {
    File file = new File(pathToFile);
    if (file.delete() == false) {
        throw new IOException();
    }
}

However, if I want to delete a file with this method, I get this error:

E/AndroidRuntime(18085): java.lang.RuntimeException: Unable to create service de.bulling.smstalkvc_offline.InstallerService: java.lang.IllegalArgumentException: File /mnt/sdcard/voicefiles.zip contains a path separator
E/AndroidRuntime(18085):    at android.app.ActivityThread.handleCreateService(ActivityThread.java:2969)
E/AndroidRuntime(18085):    at android.app.ActivityThread.access$3300(ActivityThread.java:125)
E/AndroidRuntime(18085):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2087)
E/AndroidRuntime(18085):    at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime(18085):    at android.os.Looper.loop(Looper.java:123)
E/AndroidRuntime(18085):    at android.app.ActivityThread.main(ActivityThread.java:4627)
E/AndroidRuntime(18085):    at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(18085):    at java.lang.reflect.Method.invoke(Method.java:521)
E/AndroidRuntime(18085):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
E/AndroidRuntime(18085):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
E/AndroidRuntime(18085):    at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime(18085): Caused by: java.lang.IllegalArgumentException: File /mnt/sdcard/voicefiles.zip contains a path separator
E/AndroidRuntime(18085):    at android.app.ContextImpl.makeFilename(ContextImpl.java:1602)
E/AndroidRuntime(18085):    at android.app.ContextImpl.deleteFile(ContextImpl.java:428)
E/AndroidRuntime(18085):    at android.content.ContextWrapper.deleteFile(ContextWrapper.java:163)
E/AndroidRuntime(18085):    at de.bulling.smstalkvc_offline.InstallerService.onCreate(InstallerService.java:30)
E/AndroidRuntime(18085):    at android.app.ActivityThread.handleCreateService(ActivityThread.java:2959)
E/AndroidRuntime(18085):    ... 10 more

What did I do wrong?

Force
  • 6,312
  • 7
  • 54
  • 85
  • Related post:http://stackoverflow.com/questions/5963535/java-lang-illegalargumentexception-contains-a-path-separator – Mister Smith Dec 12 '11 at 12:42
  • 1
    No, I am using a `new File()`, not a `openFileInput`, so this is not the same question. – Force Dec 12 '11 at 12:46
  • I did'n say "duplicate question" but "related". It may be helpful for you to read similar issues. Also it has the benefit to appear in Linked bar in every question. – Mister Smith Dec 12 '11 at 13:02

3 Answers3

4

It seems that you are calling wrong function at InstallerService.java line 30. Make sure you call your own deleteFile by using class name before as: YourClass.deleteFile();

I think somehow ContextWrapper.deleteFile() is called which doesn't accept path separator.

Caner
  • 57,267
  • 35
  • 174
  • 180
1

The error does not appear in the code you show. The error is in your InstallerService.java line 30:

E/AndroidRuntime(18085): at android.content.ContextWrapper.deleteFile(ContextWrapper.java:163) E/AndroidRuntime(18085): at de.bulling.smstalkvc_offline.InstallerService.onCreate(InstallerService.java:30)

The call to deleteFile() should not have path separators as its description says "Delete the given private file associated with this Context's application package.". I.e., its use is only to delete files inside that private subdirectory of where your app is installed.

Jacob Nordfalk
  • 3,533
  • 1
  • 21
  • 21
  • Well, Line 30 is a call to this function- So if you're saying I can't use File.delete(), how can I delete a file from a SD Card? – Force Dec 12 '11 at 12:40
  • Rename your method to something alse than deleteFile(). It's clashing with a buildin method. – Jacob Nordfalk Dec 12 '11 at 12:57
0

You are overriding a method in the class ContextImpl (from which your activity extends) with the same name and signature. This method is being called when the activity is created, I guess.

Try changing "deleteFile" for other name.

Mister Smith
  • 27,417
  • 21
  • 110
  • 193