Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete file in internal memory from Android device?

Tags:

java

file

android

I am trying to delete a file stored in internal memory. The file does gets deleted by using

activity.deleteFile(filename);

but only in emulator. On the actual device the method always returns false. When I try to access the file from adb shell there is permission denied being displayed. So, I guess there is permission related issue with deleting the files in internal memory.

Can someone let me know how to actually delete the file from internal memory in Android?

like image 977
sunil Avatar asked Mar 18 '26 19:03

sunil


2 Answers

If you're talking about just any file in the file system... Does this not work?

if (new File("fileUrl").delete()) {
  // Deleted
} else {
  // Not deleted
}
like image 99
Michell Bak Avatar answered Mar 21 '26 07:03

Michell Bak


Due to security constraints you can only delete files that were created by your app. You also can not delete files that are part of your app package (apk), i.e. files in /res, /assets, etc..

like image 44
Peter Knego Avatar answered Mar 21 '26 09:03

Peter Knego



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!