Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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!

like image 289
Daniel Schuler Avatar asked Jun 25 '11 20:06

Daniel Schuler


People also ask

What is BitmapFactory?

The BitmapFactory creates a bitmap object with this image and we use the ImageView. setImageBitmap() method to update the ImageView component. 2) From an Input stream. Use BitmapFactory. decodeStream() to convert a BufferedInputStream into a bitmap object.

What is bitmap config ARGB_8888?

Config ARGB_8888. Each pixel is stored on 4 bytes. Each channel (RGB and alpha for translucency) is stored with 8 bits of precision (256 possible values.) This configuration is very flexible and offers the best quality.

What is DecodeStream in Android?

DecodeStream(Stream)Decode an input stream into a bitmap.


2 Answers

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);
like image 194
Mihai Avatar answered Sep 18 '22 18:09

Mihai


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)

like image 22
Jarek Potiuk Avatar answered Sep 18 '22 18:09

Jarek Potiuk