Android: How to delete files from external storage?

enter image description hereI am implementing an application that creates and creates files in external storage. Can you guys help me, how can we remove this?

Thanks Chandra

Edit-1: I added the following code, and still I have the same problem. Please take a look at 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 LogCat, I get an application that can delete a file, and the result is false . I attached my screenshot after doing this. Please study this. And offer me.

+5
source share
8 answers
File file = new File(selectedFilePath);
boolean deleted = file.delete();

where selectedFilePath is the path to the file you want to delete, for example:

/sdcard/YourCustomDirectory/ExampleFile.mp3
+10
source

, :

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

, AndroidManifest.xml, delete() false.

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

,

ex "/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) {

        }
    }

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

: /mnt/Pictures/SampleImage.png

+1
public void deleteOnExit ()

, .

, Android VM, , . :

Use the finally clause to manually call delete (). Save your own set of files for deletion and process it at the appropriate point in the application life cycle. Use the Unix trick to delete the file as soon as all its readers and writers have opened it. No new readers / writers will be able to access the file, but all existing ones will still have access until the latter closes the file.

+1
source

It is very simple:

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

Hope this helps you.

+1
source

Try this, it will delete the 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());
    }
}
+1
source

All Articles