I'm on 4.4.2, trying to delete a file (image) via uri. Here's my code:
File file = new File(uri.getPath());
boolean deleted = file.delete();
if(!deleted){
boolean deleted2 = file.getCanonicalFile().delete();
if(!deleted2){
boolean deleted3 = getApplicationContext().deleteFile(file.getName());
}
}
Right now, none of these delete functions is actually deleting the file. I also have this in my AndroidManifest.xml:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE" />
Why don't you test this with this code:
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());
}
}
I think part of the problem is you never try to delete the file, you just keep creating a variable that has a method call.
So in your case you could try:
File file = new File(uri.getPath());
file.delete();
if(file.exists()){
file.getCanonicalFile().delete();
if(file.exists()){
getApplicationContext().deleteFile(file.getName());
}
}
However I think that's a little overkill.
You added a comment that you are using an external directory rather than a uri. So instead you should add something like:
String root = Environment.getExternalStorageDirectory().toString();
File file = new File(root + "/images/media/2918");
Then try to delete the file.