Reputation: 6382
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?
Upvotes: 1
Views: 6917
Reputation: 28168
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.
Upvotes: 0
Reputation: 59168
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.
Upvotes: 4
Reputation: 3533
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.
Upvotes: 1