views:

1652

answers:

5

I decode bitmaps from the SD card using BitmapFactory.decodeFile. Sometimes the bitmaps are bigger than what the application needs or that the heap allows, so I use BitmapFactory.Options.inSampleSize to request a subsampled (smaller) bitmap.

The problem is that the platform does not enforce the exact value of inSampleSize, and I sometimes end up with a bitmap either too small, or still too big for the available memory.

From http://developer.android.com/reference/android/graphics/BitmapFactory.Options.html#inSampleSize:

Note: the decoder will try to fulfill this request, but the resulting bitmap may have different dimensions that precisely what has been requested. Also, powers of 2 are often faster/easier for the decoder to honor.

How should I decode bitmaps from the SD card to get a bitmap of the exact size I need while consuming as little memory as possible to decode it?

Edit:

Current source code:

BitmapFactory.Options bounds = new BitmapFactory.Options();
this.bounds.inJustDecodeBounds = true;
BitmapFactory.decodeFile(filePath, bounds);
if (bounds.outWidth == -1) { // TODO: Error }
int width = bounds.outWidth;
int height = bounds.outHeight;
boolean withinBounds = width <= maxWidth && height <= maxHeight;
if (!withinBounds) {
    int newWidth = calculateNewWidth(int width, int height);
    float sampleSizeF = (float) width / (float) newWidth;
    int sampleSize = Math.round(sampleSizeF);
    BitmapFactory.Options resample = new BitmapFactory.Options();
    resample.inSampleSize = sampleSize;
    bitmap = BitmapFactory.decodeFile(filePath, resample);
}
A: 

Simple solution...

http://developer.android.com/reference/android/graphics/BitmapFactory.Options.html#inJustDecodeBounds

Set the inJustDecodeBounds to TRUE and load the file as it is.

The image won't be loaded into memory. But the outheight and outwidth properties of BitmapFactory.Options will contain the actual size params of the image specified. Calculate how much u want to subsample it. i.e. 1/2 or 1/4 or 1/8 etc. and assign 2/4/8 etc. accordingly to the inSampleSize.

Now set inJustDecodeBounds to FALSE and call BitmapFactory.decodeFile to load the exact sized image.

GOOD LUCK!!

CVS-2600Hertz
That's what I'm doing already. The problem is not finding the right value for inSampleSize. The problem is that inSampleSize is not always honored by the platform, so I might get a smaller or larger bitmap than needed anyway.
hgpc
inSampleSize ought to return U a sub-sampled image. i.e. the exact dimensions would depend on the original image U are loading. it would be half/quarter/one-eighth etc. relative to the original image.U could load the image-subsample thats closest to ur needs and appropriately stretch it while displaying.What are U doing next to display the image?? maybe i could help there. Hmmm??...
CVS-2600Hertz
That's exactly what I'm doing right now, and because inSampleSize is not always honored the results are not always optimal. I'm looking for an alternative, if possible.
hgpc
what do U mean by non-optimal. Are the image being stretched too awkwardly or what??...
CVS-2600Hertz
No, the bitmap simply isn't of the requested size sometimes. It's either too big, or too small. If it's too big, the device might run out of memory. If it's too small, the resolution is below what's expected.
hgpc
could U post ur code somewhere and link it here. I'll go thru it and get back to U ASAP...
CVS-2600Hertz
Thanks. Source code posted.
hgpc
may i know what you are trying inside calculateNewWidth() function?? i.e. how does it calculate the newWidth param that it returns??
CVS-2600Hertz
+1  A: 

Since the API already states that the sample size may or may not be honored. So I guess ur out of luck while using BitmapFactory .

But the way out would be using your own bitmap reader. But I would suggest that you stick with BitmapFactory as its fairly well tested and standardized.

I would try and not worry too much about little extra memory consumption, but try and find out why its not honoring the values. Sadly I have not idea about that :(

the100rabh
Maybe there's an alternative using BitmapFactory, but in a different way. Is it possible to load a Bitmap in chunks, rescale them in memory and then join them? It would be slower, sure, but it would provide an exact result.
hgpc
What do u want to optimize, space or resolution ??
the100rabh
Resolution and heap memory are the main concerns. Obtaining the desired bitmap should take as little memory as possible (aside from the bitmap, of course).
hgpc
I would suggest you to not worry too much as these libraries often have to use extra space to optimised for speed
the100rabh
A: 

Since I use inSampleSize I never face out of memory issues any more. So inSampleSize works quite stable for me. I would recommend you to double check the issue. The size may be not exacly the same as you requested. But it should be reduced anyway and there should be no more "Out of memory". I would also recommend to post your code snippet here, maybe there's something wrong with it.

Fedor
Thanks Fedor. Source code posted.
hgpc
A: 

As the100rabh said, if you use BitmapFactory you don't really have much choice. However, bitmap decoding is really simple, and depending on the scaling algorithm you want to use, it's usually fairly simple to scale it on-the-fly without having to read large parts of the original bitmap into memory (in general, you'll only need double the memory required for 1-2 lines of the original bitmap).

Tal Pressman
A: 

You are on the right track, however you are trying to do two things at once: read the file in and scale it to the appropriate size.

The first step is to read the file to a Bitmap slightly bigger than you require, using BitmapFactory.Options.inSampleSize to ensure that you do not consume excessive memory reading a large bitmap when all you want is a smaller thumbnail or screen resolution image.

The second step is to call Bitmap.createScaledBitmap() to create a new bitmap to the exact resolution you require.

Make sure you clean up after the temporary bitmap to reclaim its memory. (Either let the variable go out of scope and let the GC deal with it, or call .recycle() on it if you are loading lots of images and are running tight on memory.)

sirdodger
I'm going to try this. Thanks.
hgpc