Delete file in internal memory from Android device?

I am trying to delete a file stored in internal memory. File is deleted using

activity.deleteFile(filename); 

but only in the emulator. On the device itself, the method always returns false. When I try to access the file from adb shell, permission is not displayed. So, I think there is a resolution issue when deleting files in internal memory.

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

+2
source share
4 answers

If you are talking only about any file in the file system ... Does this not work?

 if (new File("fileUrl").delete()) { // Deleted } else { // Not deleted } 
+4
source

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

+4
source

Here, FileName is the name of the file you want to delete, and without the path separator. This means that FileName should not contain a path separator, such as "/". The file must be created by your application. My problem was resolved by this code.

if(getApplicationContext().deleteFile(FileName)) { Toast.makeText(getApplicationContext(),"File Deleted",Toast.LENGTH_SHORT).show(); } else { Toast.makeText(getApplicationContext(),"Not Deleted",Toast.LENGTH_SHORT).show(); }

+3
source

You should use:

 _context.openFileOutput(fileName, Context.MODE_WORLD_READABLE); 

when writing a file

0
source

All Articles