Recycle Imageview's Bitmap

Does ImageView.setImageBitmap() recycle the previously set bitmap?

ImageView doesnt release the bitmaps automatically

It happens as explained by @Vipul

Bitmaps reference must be released by calling bitmap.recycle()

When you want to assign another bitmap to the ImageView recycle the previous by calling

((BitmapDrawable)imageView.getDrawable()).getBitmap().recycle();

Take a look at this

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

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

How to effectively recycle a Bitmap which is created as per below code?

In this particular case, no, you shouldn't call recycle(); the ImageView will call recycle() when it is done with it. This has been true for a while, ICS did nothing to change this fact.

You need to call recycle() when your code is done with the image. For example if you were applying 10 filters to one image and generating a new Bitmap on each step, you SHOULD call recycle() on the old Bitmap after each step.

That said, you can't have an unlimited number of Bitmaps at the same time, especially large ones. That's when you need to be clever and load/unload dynamically.

Does the RecyclerView recycle Bitmaps stored within ImageViews

No, it doesn't. Viewholder is intended to prevent the extra resources for findViewByid look ups. i dont think it does anything extra than that!



Related Topics



Leave a reply



Submit