Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to delete file created by me in Android Q?

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?

like image 365
VolodymyrH Avatar asked Oct 16 '25 02:10

VolodymyrH


1 Answers

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.

like image 103
greywolf82 Avatar answered Oct 18 '25 16:10

greywolf82



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!