gpt4 book ai didi

android - new File() 给出 IllegalArgumentException : File contains a path separator

转载 作者:行者123 更新时间:2023-11-29 15:29:25 26 4
gpt4 key购买 nike

我写了下面的代码来从 SD 卡中删除一个文件:

/**
* 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();
}
}

但是,如果我想用这种方法删除一个文件,我会得到这个错误:

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

我做错了什么?

最佳答案

您似乎在 InstallerService.java 第 30 行调用了错误的函数。请确保在调用您自己的 deleteFile 之前使用类名:YourClass .deleteFile();

我认为 ContextWrapper.deleteFile() 以某种方式被调用,它不接受路径分隔符。

关于android - new File() 给出 IllegalArgumentException : File contains a path separator,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8474336/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com