Android:如何从外部存储中删除文件?

Cha*_*har 5 android

在此输入图像描述我实现了一个在外部存储中生成和创建文件的应用程序.你能帮帮我,我们怎么能删除它?

谢谢钱德拉

编辑-1: 我添加了以下代码,但我仍然遇到同样的问题,请看下面的代码,

String fullPath = "/mnt/sdcard/";
System.out.println(fullPath);
try{
    File file = new File(fullPath, "audio.mp3");
    if(file.exists()){
        boolean result = file.delete();
        System.out.println("Application able to delete the file and result is: " + result);
        // file.delete();
    }else{
        System.out.println("Application doesn't able to delete the file");
    }
}catch (Exception e){
    Log.e("App", "Exception while deleting file " + e.getMessage());
}
Run Code Online (Sandbox Code Playgroud)

在LogCat中我得到Application能够删除文件,结果是:false.执行此操作后,我附加了屏幕截图.请仔细看看.并建议我.

jlo*_*pez 11

File file = new File(selectedFilePath);
boolean deleted = file.delete();
Run Code Online (Sandbox Code Playgroud)

其中selectedFilePath是您要删除的文件的路径 - 例如:

/sdcard/YourCustomDirectory/ExampleFile.mp3
Run Code Online (Sandbox Code Playgroud)


Jpa*_*lle 5

我认为您忘记将写入权限放入 AndroidManifest.xml 文件中,这就是为什么 delete() 总是返回 false 的原因。

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Run Code Online (Sandbox Code Playgroud)


lok*_*oko 1

File file = new File(selectedFilePath);
boolean deleted = file.delete();
Run Code Online (Sandbox Code Playgroud)

路径会是这样的:/mnt/Pictures/SampleImage.png