sunil
sunil

Reputation: 9681

Delete file in internal memory from Android device?

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?

Upvotes: 2

Views: 8794

Answers (4)

SNDM
SNDM

Reputation: 31

Here FileName is the name of File which you want to delete and without path separator.that mean FileName should not contain path separator like "/". And File should be created by your application. My Problem was solved by that code..

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

Upvotes: 3

ZiviMagic
ZiviMagic

Reputation: 19

You should use:

_context.openFileOutput(fileName, Context.MODE_WORLD_READABLE);

when writing the file

Upvotes: 0

Michell Bak
Michell Bak

Reputation: 13242

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
}

Upvotes: 4

Peter Knego
Peter Knego

Reputation: 80330

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..

Upvotes: 4

Related Questions