简体   繁体   English

从Android设备删除内存中的文件?

[英]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. 在实际设备上,该方法始终返回false。 When I try to access the file from adb shell there is permission denied being displayed. 当我尝试从adb shell访问该文件时,会显示权限被拒绝。 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? 有人能告诉我如何从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.. 您也无法删除属于您的应用程序包(apk)的文件,即/res/assets等中的文件。

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 "/". 这里FileName是要删除的文件的名称,没有路径分隔符。这意味着FileName不应包含路径分隔符,如“/”。 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 在写文件时

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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