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.
where selectedFilePath is the path of the file you want to delete - for example:
Change for Android 4.4+
Apps are not allowed to write (delete, modify ...) to external storage except to their package-specific directories.
As Android documentation states:
However nasty workaround exists (see code below). Tested on Samsung Galaxy S4, but this fix does't work on all devices. Also I wouldn’t count on this workaround being available in future versions of Android.
There is a great article explaining (4.4+) external storage permissions change.
You can read more about workaround here. Workaround source code is from this site.
Recursively delete all children of the file ...
This Code will Help you.. And In Android Manifest You have to get Permission to make modification..
Android Context has the following method:
I believe this will do what you want with the right App premissions as listed above.
Try this.
from Apache Commons