简体   繁体   English

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

[英]Android: How to remove files from External storage?

在此处输入图片说明

I am implementing an application which generates and create files in External storage.我正在实现一个在外部存储中生成和创建文件的应用程序。 How can I remove that?我怎样才能删除它?

Edit编辑

I added following code and still I am getting the same problem.我添加了以下代码,但仍然遇到同样的问题。 See my code below:请参阅下面的代码:

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

In the LogCat I am getting Application able to delete the file and result is:false .在 LogCat 中,我让应用程序能够删除文件,结果是:false I attached my screen shot after executing this.执行此操作后,我附上了我的屏幕截图。

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

where selectedFilePath is the path of the file you want to delete - for example:其中 selectedFilePath 是您要删除的文件的路径 - 例如:

/sdcard/YourCustomDirectory/ExampleFile.mp3

This is another way just pass your directory name to delete content of it这是另一种方式,只需传递您的目录名称即可删除其中的内容

ex "/sdcard/abc/yourdata"例如"/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) {

        }
    }

}

我认为您忘记在 AndroidManifest.xml 文件中设置写入权限,这就是 delete() 始终返回 false 的原因。

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

Try the following code to delete the automatically created files and when you don't know where it is stored:尝试以下代码删除自动创建的文件以及当您不知道它的存储位置时:

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

Its very simple:它非常简单:

File file = new File(YOUR_IMAGE_PATH).delete();
if(file.exists())
    file.delete();

Hope it will help you.希望它会帮助你。

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

path would be something like : /mnt/Pictures/SampleImage.png路径将类似于: /mnt/Pictures/SampleImage.png

public void deleteOnExit ()

Schedules this file to be automatically deleted when the VM terminates normally.安排此文件在 VM 正常终止时自动删除。

Note that on Android, the application lifecycle does not include VM termination, so calling this method will not ensure that files are deleted.请注意,在Android上,应用程序生命周期不包括VM终止,因此调用此方法并不能确保文件被删除。 Instead, you should use the most appropriate out of:相反,您应该使用最合适的:

Use a finally clause to manually invoke delete().使用 finally 子句手动调用 delete()。 Maintain your own set of files to delete, and process it at an appropriate point in your application's lifecycle.维护您自己的一组要删除的文件,并在应用程序生命周期中的适当时间点对其进行处理。 Use the Unix trick of deleting the file as soon as all readers and writers have opened it.使用 Unix 技巧,在所有读者和作者打开文件后立即删除该文件。 No new readers/writers will be able to access the file, but all existing ones will still have access until the last one closes the file.没有新的读者/作者将能够访问该文件,但所有现有的读者/作者仍然可以访问,直到最后一个关闭文件。

Try this it will delete file from external storage.试试这个它会从外部存储中删除文件。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM