How to delete a file from SD card?


Question

I am creating a file to send as an attachment to an email. Now I want to delete the image after sending the email. Is there a way to delete the file?

I have tried myFile.delete(); but it didn't delete the file.


I'm using this code for Android, so the programming language is Java using the usual Android ways to access the SD card. I am deleting the file in the onActivityResult method, when an Intent is returned to the screen after sending an email.

1
129
3/27/2013 7:19:32 PM

Accepted Answer

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

357
8/10/2009 9:14:48 AM

Also you have to give permission if you are using >1.6 SDK

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

in AndroidManifest.xml file


Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon