Delete file from internal storage
I'm trying to delete images stored in internal storage. I've come up with this so far:
File dir = getFilesDir();
File file = new File(dir, id+".jpg");
boolean deleted = file.delete();
And this is from another question, which was answered with:
File dir = getFilesDir();
File file = new File(dir, "my_filename");
boolean deleted = file.delete();
My example always returns false. I can see the file fx 2930.jpg
in DDMS in eclipse.
The getFilesDir()
somehow didn't work.
Using a method, which returns the entire path and filename gave the desired result. Here is the code:
File file = new File(inputHandle.getImgPath(id));
boolean deleted = file.delete();
Have you tried Context.deleteFile() ?
Java
File file = new File(photoPath);
file.delete();
MediaScannerConnection.scanFile(context,
new String[]{file.toString()},
new String[]{file.getName()},null);
Kotlin
val file = File(photoPath)
file.delete()
MediaScannerConnection.scanFile(context, arrayOf(file.toString()),
arrayOf(file.getName()), null)