Background
Using the Volley library's NetworkImageView is a convenient way to handle showing images from the web.
However, it has some bugs (as i've written here).
the problem
One of the issues that you can have by using it is that it doesn't decode the images from the web in a memory efficient way.
This means that if you use a gridView with multiple NetworkImageView in it, and each shows an image that has an unknown resolution (could be small, could be large), you would end up having an OOM .
as an example, you can set the url of this object to be this one and see for yourself how much memory the app uses after showing the bitmap, compared to how much it used before.
The question
How can i modify the way that NetworkImageView decodes the bitmap ?
One way I could change it is to make it decode the bitmap while downscaling it to the needed size (or at least set the max of it to the screen size), for example using this method of downscaling .
Volley has a built in method for fitting an image to a given width and height like you mentioned. You need to stop using the convenience methods of loading images provided by
NetworkImageView
which don't use it. I suggest using the following methods to decrease the chance for OOM errors:NetworkImageView
. Use a regularImageView
and implement the listener to apply the image when it is available. This is a prerequisite for step 2. Using aNetworkImageView
with theget()
method may lead to problems in my experience`.ImageLoader
and use theget()
method which receives anImageRequest
. Use the optional constructor that takes in a maxHeight and maxWidth as parameters if you can.get()
method in theImageLoader
, save theImageContainer
reference that method returns so you'll be able to cancel a request if the view is recycled before the request completes.ImageCache
in theImageLoader
constructor. That'll lower the redundancy in decoding bitmaps that are already available.recycle()
method on the bitmaps, but be careful not to recycle the ones you might still need.EDIT: Added Code Samples
Code snippet for (2) + (4)
Code snippet for (3) assuming the ViewHolder pattern and
imageContainer
is a member of theViewHolder
class. The principal applies to any architecture.The default image loader (you can do what you here):