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

2

11

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!

Corium answered 25/6, 2011 at 20:3 Comment(0)
Y
23

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);
Yuonneyup answered 26/4, 2012 at 10:9 Comment(1)
RGBA8888 not ARGBHolt
I
1

When you have bitmap you can call copy method on it specifying BitmapConfig which is basically what you want. http://developer.android.com/reference/android/graphics/Bitmap.html#copy(android.graphics.Bitmap.Config,boolean)

Insignificance answered 25/6, 2011 at 20:39 Comment(1)
I was hoping there would be a way to do this during decode, instead of making a copy later. It looks like this is the only way though.Corium

© 2022 - 2024 — McMap. All rights reserved.