0

I am creating an app with a shared transition animation. For this I create an intent with an image. But the image gets stored on the device. But how can I delete this aftere the transition?

Here I share the transition:

Bitmap photo = ((BitmapDrawable) drawable).getBitmap();
String path = MediaStore.Images.Media.insertImage(getContentResolver(), photo, "pic", null);
Uri uri = Uri.parse(path);
intent.putExtra(Intent.EXTRA_STREAM, uri);

And here I recieve the uri:

pic = extras.getParcelable(Intent.EXTRA_STREAM);

And here I try to delete the image, but it doesn't find it:

File fdlt = new File(pic.getPath());
        if (fdlt.exists()) {
            if (fdlt.delete()) {
                Log.e("DELETED", "DELETED");
            } else {
                Log.e("NOT DELETED", "NOT DELETED");
            }
        } else {
            Log.e("FILE NOT FOUND", "FILE NOT FOUND");
        }

Example path of an image: /external/images/media/2750

user6586661
  • 432
  • 1
  • 11
  • 24

2 Answers2

1
File fdlt = new File(pic.getPath());
        if (fdlt.exists()) {
            if (fdlt.delete()) {
                Log.e("DELETED", "DELETED");
            } else {
                Log.e("NOT DELETED", "NOT DELETED");
            }
        } else {
            Log.e("FILE NOT FOUND", "FILE NOT FOUND");
        }

Replace your this code with below one.

        int size = 0;
        size = this.getContentResolver().delete(pic,
                null, null);
        if (size == 0) {
            Log.e("NOT DELETED", "NOT DELETED");
        } else {
            Log.e("DELETED", "DELETED");
        }
Sohail Zahid
  • 8,099
  • 2
  • 25
  • 41
0

You can store the image in your app's cache dir, and let Android take care of deleting it: Creating temporary files in Android

Community
  • 1
  • 1
Vaiden
  • 15,728
  • 7
  • 61
  • 91