Search code examples
androidfiledirectorydelete-fileandroid-file

How to delete a file/folder in a SDCARD under Android Platform?


I want to know how to delete a file/folder in SD Card. I tried the code below:

File folder = Environment.getExternalStorageDirectory();
String fileName = folder.getPath() + "/LOST.DIR/ppt52.ppt";
File myFile = new File(fileName);
if(myFile.exists()) {
    myFile.delete();
}

but it didn't work.


Solution

  • Maybe the folder you are trying to access is Protected. So try to change the access mode before accessing it.

    Or you are missing the following permission,

    uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE