views:

923

answers:

2

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 SoftReferences 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 BitmapFactory.decodeStream(InputStream) 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?

+10  A: 

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.

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

Samuh
+1 for answering your own question when you found out the answer; think of those who google and find your question, but no answer.
Will
+1  A: 

What I've done myself is :

  • use inJustDecodeBounds to get the original size of the image
  • have a fixed maximum Surface for loading the Bitmap (say 1Mpixels)
  • check if the image surface is below the limit, if yes, then load it directly
  • if not compute the ideal width and height at which you should load the Bitmap to stay below the max surface (there is some simple maths to do here). This give you a float ratio that you want to apply when loading the bitmap
  • now you want to translate this ratio to a suitable inSampleSize (a power of 2 that doesn't degrade the image quality). I use this function :
int k = Integer.highestOneBit((int)Math.floor(ratio));
if(k==0) return 1;
else return k;
  • then because the Bitmap will have been loaded with a slightly higher resolution than the max surface (because you had to use a smaller power of 2), you'll have to resize the Bitmap, but it will be much faster.
Greg
thanks for adding the routine to calculate sample size. It augments the thread with a piece of important information.
Samuh