How to destroy a drawable inside an ImageView if we don't need it?
Asked Answered
P

2

5

This question is related with Do we have to explicitly recycle the bitmap if we don't need it?.

There is an ImageView have a drawable, when user clicks a button, it will assign a new drawable to the ImageView.

Do we have to destroy the old drawable belongs to the ImageView, and how?

Drawable oriDrawable = imageView.getDrawable()

// set callback to null
oriDrawable.setCallback(null);

// get the bitmap and recycle it
((BitmapDrawable)oriDrawable).getBitmap().recycle();

Is the code above correct? What's the best solution?

Platas answered 21/9, 2012 at 16:1 Comment(0)
L
4

Is this just a general question or are you running out of memory? I would not make this optimisation until you really have a problem.

In general if you are loading bitmaps from drawable folders that are not large (large as in megabytes) then you should not really run into a problem.

First you need to make sure the assets you are loading are optimal for where you display them, for example there is no point in setting an ImageView to an image thats 1024 x 1024 in size if the area you display the image is a size of 64x64.

Breaking the bitmap budget is usually caused by loading in images of an unknown size or just simply getting the image sizes wrong as described above, swapping an ImageView frequently will generally not give you an issue with optimal sized images.

There is a great article in Android Training that covers loading bitmaps optimally http://developer.android.com/training/displaying-bitmaps/load-bitmap.html

Hope that helps

Latinalatinate answered 21/9, 2012 at 16:20 Comment(2)
I do have OutOfMemoryException, and have fixed it as the suggestion of the url. But I'm sure if I need to recycle the unused drawable as much as possible or it will be automatically destroyed when I assign a new drawable.Platas
I would first try to see if you get the issue without recycling because they will be recycled with a GC. Just make sure there are no extra references to the drawables or bitmaps to make them GC eligible.Latinalatinate
I
12

You could try using something like:

Drawable drawable = imageView.getDrawable();
if (drawable instanceof BitmapDrawable) {
    BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
    Bitmap bitmap = bitmapDrawable.getBitmap();
    bitmap.recycle();
}

Where imageView is your ImageView.

Original answer here.

Il answered 21/9, 2012 at 16:7 Comment(6)
Actually it does exactly what do you suggestSirrah
@blackbelt I'm not sure what you mean by thatIl
Thanks. And I'm not sure do we have to recycle big drawable as much as possible? If yes, why doesn't android provide a recycle method to drawable?Platas
And is oriDrawable.setCallback(null) useful here?Platas
How do you do this without getting java.lang.RuntimeException: Canvas: trying to use a recycled bitmap. ?Acicular
I'm so sad. After checking with the profiler, the bitmap data are not recycled with this instruction the bitmap.recycle().Deena
L
4

Is this just a general question or are you running out of memory? I would not make this optimisation until you really have a problem.

In general if you are loading bitmaps from drawable folders that are not large (large as in megabytes) then you should not really run into a problem.

First you need to make sure the assets you are loading are optimal for where you display them, for example there is no point in setting an ImageView to an image thats 1024 x 1024 in size if the area you display the image is a size of 64x64.

Breaking the bitmap budget is usually caused by loading in images of an unknown size or just simply getting the image sizes wrong as described above, swapping an ImageView frequently will generally not give you an issue with optimal sized images.

There is a great article in Android Training that covers loading bitmaps optimally http://developer.android.com/training/displaying-bitmaps/load-bitmap.html

Hope that helps

Latinalatinate answered 21/9, 2012 at 16:20 Comment(2)
I do have OutOfMemoryException, and have fixed it as the suggestion of the url. But I'm sure if I need to recycle the unused drawable as much as possible or it will be automatically destroyed when I assign a new drawable.Platas
I would first try to see if you get the issue without recycling because they will be recycled with a GC. Just make sure there are no extra references to the drawables or bitmaps to make them GC eligible.Latinalatinate

© 2022 - 2024 — McMap. All rights reserved.