Samuh Samuh - 1 year ago 93
Android Question

Handling large Bitmaps

I have a bunch of image URLs. I have to download these images and display them in my application one-by-one. I am saving the images in a Collection using

and also on Sdcard to avoid refetches and improve user experience.

The problem is I dont know anything about the size of the bitmaps. And as it turns out, I am getting OutOfMemoryExceptions sporadically, when I am using
method. So, I chose to downsample the images using BitmapFactory Options(sample size=2). This gave a better output: no OOMs, but this affects the quality of smaller images.

How should I handle such cases? Is there a way to selectively downsample only high resolution images?

Answer Source

There is an option in BitmapFactory.Options class (one I overlooked) named inJustDecodeBounds, javadoc of which reads:

If set to true, the decoder will return null (no bitmap), but the out... fields will still be set, allowing the caller to query the bitmap without having to allocate the memory for its pixels.

I used it to find out the actual size of the Bitmap and then chose to down sample it using inSampleSize option. This at least avoids any OOM errors while decoding the file.

1. Handling larger Bitmaps
2. How do I get Bitmap info before I decode

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download