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

9

enter image description here

我正在实现一个在外部存储中生成和创建文件的应用程序。如何删除它?

编辑

我添加了以下代码,但仍然遇到同样的问题。请查看我的代码:

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

在LogCat中,我看到了应用程序能够删除文件但结果为false的信息。执行后,我附上了屏幕截图。
7个回答

5
我觉得您可能忘记在AndroidManifest.xml文件中设置写入权限,这就是为什么delete()始终返回false的原因。
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

自 Android KitKat 以来有一些变化,未来的读者可以使用以下链接作为参考的一种来源:https://dev59.com/4FcO5IYBdhLWcg3wxEYP - Gleichmut

4
这是另一种方法,只需将您的目录名称传递给它以删除其内容。
例如:"/sdcard/abc/yourdata"
public void deleteFiles(String path) {      
    File file = new File(path);

    if (file.exists()) {
        String deleteCmd = "rm -r " + path;
        Runtime runtime = Runtime.getRuntime();
        try {
            runtime.exec(deleteCmd);
        } catch (IOException e) {

        }
    }

}

4
非常简单:
File file = new File(YOUR_IMAGE_PATH).delete();
if(file.exists())
    file.delete();

希望这能对您有所帮助。


2
尝试以下代码来删除自动创建的文件,当你不知道它存储在哪里时:
public void clearApplicationData() {
    File cache = getCacheDir();
    File appDir = new File(cache.getParent());
    if (appDir.exists()) {
        String[] children = appDir.list();
        for (String s : children) {
            if (!s.equals("lib")) {
                deleteDir(new File(appDir, s));
                Log.i("TAG",
                        "**************** File /data/data/APP_PACKAGE/" + s
                                + " DELETED *******************");
            }
        }
    }
}

public static boolean deleteDir(File dir) {
    if (dir != null && dir.isDirectory()) {
        String[] children = dir.list();
        for (int i = 0; i < children.length; i++) {
            boolean success = deleteDir(new File(dir, children[i]));
            if (!success) {
                return false;
            }
        }
    }

    return dir.delete();
}

2

尝试这个,它将从外部存储中删除文件。

public void deleteFromExternalStorage(String fileName) {
    String fullPath = "/mnt/sdcard/";
    try
    {
        File file = new File(fullPath, fileName);
        if(file.exists())
            file.delete();
    }
    catch (Exception e)
    {
        Log.e("App", "Exception while deleting file " + e.getMessage());
    }
}

我使用了上述代码,并通过调试模式进行了测试,实际上调试器已经执行了file.delete()。之后我打开了DDMS并检查了文件。不幸的是,文件仍然存在。你能给我建议吗?我该怎么解决这个问题? - Sekhar Bhetalam
路径为mnt/sdcard/audio.mp3。 - Sekhar Bhetalam
尝试将audio.mp3作为文件名传递。 - Ajay S
请查看我的编辑。我仍然遇到同样的问题。 - Sekhar Bhetalam

1
public void deleteOnExit ()

调度此文件在虚拟机正常终止时自动删除。

请注意,在Android上,应用程序生命周期不包括VM终止,因此调用此方法将不能确保文件被删除。相反,您应该使用以下最合适的方法之一:

使用finally子句手动调用delete()。 维护自己的要删除的文件集,并在应用程序生命周期的适当时点处理它。 使用Unix技巧,在所有读者和写入者打开文件后立即删除该文件。没有新的读者/写入者能够访问该文件,但所有现有的读者/写入者仍将可以访问该文件,直到最后一个关闭文件。


1
File file = new File(selectedFilePath);
boolean deleted = file.delete();

路径将类似于:/mnt/Pictures/SampleImage.png


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接