I'm saving an image in DCIM directory, but in cases, I need to delete it. Previously, I called just image.delete(), where image is file. But now this image is saved in another way:
contentValues.put(MediaStore.MediaColumns.DISPLAY_NAME, name);
contentValues.put(MediaStore.MediaColumns.MIME_TYPE, "image/png");
contentValues.put(MediaStore.MediaColumns.RELATIVE_PATH, "DCIM" + File.separator + IMAGES_FOLDER_NAME);
Uri imageUri = resolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, contentValues);
OutputStream fos = resolver.openOutputStream(imageUri);
boolean saved = bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
I tried to make a query with its name and call contentResolver.delete(...), but it doesn't work.
I have permission to write external storage, but I don't want to use SAF.
How can I delete such file?
You need to use the delete
method of ContentResolver
using the Uri
you got when you called insert.
Uri imageUri = resolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, contentValues);
OutputStream fos = resolver.openOutputStream(imageUri);
boolean saved = bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
......
int result = resolver.delete(imageUri, null, null);
if (result > 0) {
Log.d("Tag", "File deleted");
}
If you didn't store the Uri you need to perform a query()
, retrieve the content and then call delete
.