BitmapFactory.decodeResource() ignores inPreferredConfig option for jpg images

会有一股神秘感。 提交于 2019-12-10 09:35:48

问题


I try to load jpeg resource image to Bitmap of ARGB_8888 format:

BitmapFactory.Options opts = new BitmapFactory.Options();
opts.inPreferredConfig = Bitmap.Config.ARGB_8888;
Bitmap b = BitmapFactory.decodeResource(resources, resId, opts);
Log.d("test", b.getConfig().toString());

Here resId is the id of a jpeg image resource. And the output is "RGB_565". Tried this in emulators of android 2.2 and 2.3.

Docs of 'inPreferredConfig' say:

If this is non-null, the decoder will try to decode into this internal configuration. If it is null, or the request cannot be met, the decoder will try to pick the best matching config based on the system's screen depth, and characteristics of the original image such as if it has per-pixel alpha (requiring a config that also does). Image are loaded with the ARGB_8888 config by default.

So am I hitting the case of "the request cannot be met"? :) But I honestly can't see how it is very difficult to decode RGB_565 into an ARGB_8888.

So I thought maybe I am doing wrong or this is a bug in Android...


回答1:


When loading jpeg you must set alpha channel to true:

bitmap.setHasAlpha(true);



回答2:


After looking through the source I can only tell that the decision to follow the defined inPreferredConfig is done in native code. ref: source

I would assume because a jpg cannot have an alpha channel that it is decoding it RGB_565 because it is the most efficient config for a non-alpha image. If you really want it to be decoded to ARGB_8888 convert the image into a png.



来源:https://stackoverflow.com/questions/10413628/bitmapfactory-decoderesource-ignores-inpreferredconfig-option-for-jpg-images

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!