简体   繁体   English

在Android上从私人存储删除文件

[英]Deleting file from private storage on android

I'm trying to delete a file that I earlier created in my android app. 我正在尝试删除我先前在android应用中创建的文件。 The problem I'm having is that the file won't go away. 我遇到的问题是文件不会消失。 Even though everything seems to work. 即使一切似乎正常。

I've looked at several post here on stackoverflow, but still not solution. 我在这里查看了关于stackoverflow的几篇文章,但仍未找到解决方案。 The garbage collections was one of the hints I've found. 垃圾收集是我发现的提示之一。

System.gc();
System.out.println("Exists: "+file.exists());
System.out.println("Read: "+file.canRead());
System.out.println("Write: "+file.canWrite());
System.out.println("Deleting: " + file);
boolean r = file.delete();
System.out.println("Result of deletion: "+r);
System.gc();

And the result in the log 并将结果记录在日志中

Exists: true
Read: true
Write: true
Deleting: data/data/no.ntnu.kpro.app/files/kprothales/XOMessage/8
Result of deletion: true

Does anyone have any idea as to why it isn't removed? 有谁知道为什么不删除它?

EDIT: Lucifer: Yeah, I have set WRITE_EXTERNAL_STORAGE permission in the manifest. 编辑:路西法:是的,我在清单中设置了WRITE_EXTERNAL_STORAGE权限。 ShineDown: No, it is just a file without an extension. ShineDown:不,它只是一个没有扩展名的文件。 For now it is containing xml, but this is going to change over time, hence why I have not called it .xml. 目前它包含xml,但是随着时间的流逝将发生变化,因此为什么我没有将其命名为.xml。 Could this be a problem? 这可能是个问题吗? chintan khetiya: I believe this line is allready included in the code above. chintan khetiya:我相信上面的代码中已经包含了这一行。

check the answer here: 在这里检查答案:

Android: how to delete internal image file Android:如何删除内部图片文件

which is basically suggesting to call deleteFile: 这基本上是建议调用deleteFile:

if(activity.deleteFile(imageName)) Log.i(TAG, "Image deleted."); if(activity.deleteFile(imageName))Log.i(TAG,“图片已删除。”);

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

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