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?
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
}
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..
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();
}
You should use:
_context.openFileOutput(fileName, Context.MODE_WORLD_READABLE);
when writing the file
精彩评论