如何从 SD 卡中删除文件
我正在创建一个文件作为电子邮件的附件发送。 现在我想在发送电子邮件后删除图像。 有没有办法删除文件?
我已尝试 myFile.delete();
但它没有删除该文件。
我在 Android 上使用此代码,因此编程语言是 Java,使用通常的 Android 方式访问 SD 卡。 当发送电子邮件后将 Intent
返回到屏幕时,我正在删除 onActivityResult
方法中的文件。
I am creating a file to send as an attachment to an email. Now I want to delete the image after sending the email. Is there a way to delete the file?
I have tried myFile.delete();
but it didn't delete the file.
I'm using this code for Android, so the programming language is Java using the usual Android ways to access the SD card. I am deleting the file in the onActivityResult
method, when an Intent
is returned to the screen after sending an email.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(14)
我在 4.4 上运行的应用程序也遇到了类似的问题。 我所做的有点像黑客。
我重命名了这些文件并在我的应用程序中忽略了它们。
IE。
I had a similar issue with an application running on 4.4. What I did was sort of a hack.
I renamed the files and ignored them in my application.
ie.
这对我有用。
This worked for me.
您可以按如下方式删除文件:
You can delete a file as follow:
或者,试试这个:
Or, Try This:
其中 selectedFilePath 是要删除的文件的路径 - 例如:
where selectedFilePath is the path of the file you want to delete - for example:
使用 >1.6 SDK,则还必须授予权限
如果您在
AndroidManifest.xml
文件中Also you have to give permission if you are using >1.6 SDK
in
AndroidManifest.xml
fileAndroid 4.4+
应用程序不允许更改写入(删除、修改...)到外部存储例外到其特定于包的目录。
正如 Android 文档所述:
但是令人讨厌的解决方法存在(参见下面的代码)。在 Samsung Galaxy S4 上进行了测试,但此修复程序不适用于所有设备。而且我不会指望此解决方法会在 Android 的未来版本中提供。
解释 (4.4+) 外部存储权限更改的好文章。
您可以阅读 更多信息解决方法在这里。
解决方法源代码来自此网站。
Change for Android 4.4+
Apps are not allowed to write (delete, modify ...) to external storage except to their package-specific directories.
As Android documentation states:
However nasty workaround exists (see code below). Tested on Samsung Galaxy S4, but this fix does't work on all devices. Also I wouldn’t count on this workaround being available in future versions of Android.
There is a great article explaining (4.4+) external storage permissions change.
You can read more about workaround here.
Workaround source code is from this site.
Android Context 有以下方法:
我相信这将使用上面列出的正确的应用程序权限执行您想要的操作。
Android Context has the following method:
I believe this will do what you want with the right App premissions as listed above.
递归删除文件的所有子文件...
Recursively delete all children of the file ...
这对我有用:(从图库中删除图像)
This works for me: (Delete image from Gallery)
这段代码会帮助你..并且在Android Manifest中你必须获得进行修改的权限..
This Code will Help you.. And In Android Manifest You have to get Permission to make modification..
尝试这个。
来自阿帕奇共享资源
Try this.
from Apache Commons
抱歉:由于站点验证,我的代码之前有一个错误。
我认为很清楚...
首先您必须知道您的文件位置。
其次,,,
Environment.getExternalStorageDirectory()
是获取应用程序目录的方法。最后是处理你的文件的 File 类......
Sorry: There is a mistake in my code before because of the site validation.
I think is clear...
First you must to know your file location.
Second,,,
Environment.getExternalStorageDirectory()
is a method who gets your app directory.Lastly the class File who handle your file...