为什么不用这段代码测试这个:
File fdelete = new File(uri.getPath());
if (fdelete.exists()) {
if (fdelete.delete()) {
System.out.println("file Deleted :" + uri.getPath());
} else {
System.out.println("file not Deleted :" + uri.getPath());
}
}
我认为问题的一部分是你永远不会尝试删除文件,你只是继续创建一个具有方法调用的变量.
所以在你的情况下,你可以尝试:
File file = new File(uri.getPath());
file.delete();
if(file.exists()){
file.getCanonicalFile().delete();
if(file.exists()){
getApplicationContext().deleteFile(file.getName());
}
}
但是我认为这有点过分.
您添加了一条注释,表明您使用的是外部目录而不是uri.所以你应该添加如下内容:
String root = Environment.getExternalStorageDirectory().toString();
File file = new File(root + "/images/media/2918");
然后尝试删除该文件.