9

enter image description here

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. I attached my screen shot after executing this.

halfer
  • 19,824
  • 17
  • 99
  • 186
Sekhar Bhetalam
  • 4,501
  • 6
  • 33
  • 52

7 Answers7

5

I think you forgot to put the write permission in your AndroidManifest.xml file, that's why delete() always return false.

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Jpaille
  • 123
  • 2
  • 6
  • There are some changes since Android KitKat, future readers might use the link below as an one source for reference: https://stackoverflow.com/questions/45253756/cant-delete-file-from-external-storage-in-android-programmatically – Gleichmut Jul 29 '23 at 11:25
4

Its very simple:

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

Hope it will help you.

Rushabh Patel
  • 3,052
  • 4
  • 26
  • 58
4

This is another way just pass your directory name to delete content of it

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) {

        }
    }

}
UdayaLakmal
  • 4,035
  • 4
  • 29
  • 40
2

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();
}
TheLittleNaruto
  • 8,325
  • 4
  • 54
  • 73
saran
  • 461
  • 1
  • 6
  • 20
2

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());
    }
}
Ajay S
  • 48,003
  • 27
  • 91
  • 111
1
File file = new File(selectedFilePath);
boolean deleted = file.delete();

path would be something like : /mnt/Pictures/SampleImage.png

Chintan Khetiya
  • 15,962
  • 9
  • 47
  • 85
lokoko
  • 5,785
  • 5
  • 35
  • 68
1
public void deleteOnExit ()

Schedules this file to be automatically deleted when the VM terminates normally.

Note that on Android, the application lifecycle does not include VM termination, so calling this method will not ensure that files are deleted. Instead, you should use the most appropriate out of:

Use a finally clause to manually invoke 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. 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.

Guilherme Gregores
  • 1,050
  • 2
  • 10
  • 27