Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: How to remove files from External storage?

Tags:

android

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.

like image 815
ChandraSekhar Avatar asked Feb 06 '13 10:02

ChandraSekhar


2 Answers

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

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

/sdcard/YourCustomDirectory/ExampleFile.mp3
like image 179
jlopez Avatar answered Sep 28 '22 05:09

jlopez


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" />
like image 44
Jpaille Avatar answered Sep 28 '22 04:09

Jpaille