Decoding subsampled bitmaps in Android

Posted by hgpc on Stack Overflow See other posts from Stack Overflow or by hgpc
Published on 2010-04-14T23:22:43Z Indexed on 2010/04/14 23:23 UTC
Read the original article Hit count: 384

Filed under:
|
|
|

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?

© Stack Overflow or respective owner

Related posts about android

Related posts about bitmap