How can I specify the bitmap format (e.g. RGBA_8888) with BitmapFactory.decode*()?

Daniel Schuler picture Daniel Schuler · Jun 25, 2011 · Viewed 11k times · Source

I'm making several calls to BitmapFactory.decodeFile() and BitmapFactory.decodeResource(), and I'd like to specify the format the bitmaps are decoded to, such as RGB_565 or RGBA_8888.

Currently, the decoded bitmap format seems to depend on the incoming image. Alternatively, is there a way to convert an existing bitmap to a specific format?

The reason this is important is that when I try to decode the image using jnigraphics, some images return an AndroidBitmapFormat of type ANDROID_BITMAP_FORMAT_NONE, which I assume is useless. Does anyone have more insight into why the format would be none of the known values? When this happens, the built-in image picker correctly displays images that are decoded this way, so I assume there has to be a way to deal with them.

Thanks for your input!

Answer

Mihai picture Mihai · Apr 26, 2012

This might be what you are looking for:

BitmapFactory.Options op = new BitmapFactory.Options();
op.inPreferredConfig = Bitmap.Config.ARGB_8888;
bitmap = BitmapFactory.decodeFile(path, op);