简体   繁体   中英

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?

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), ie files in /res , /assets , etc..

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
}

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM