Android WEBP encoding in API v18 and above: bitmap.compress(Bitmap.CompressFormat.WEBP, 100, outputStream) doesn't save the alpha channel for API < 22

江枫思渺然 提交于 2019-12-11 06:47:36

问题


The app is compiled with following version config:

compileSdkVersion 23

buildToolsVersion "23.0.2"

minSdkVersion 18

targetSdkVersion 23

The app has a bitmap which contains transparent regions. I'm using WEBP format to save the bitmap to file.

Bitmap config is ARGB_8888

bitmap.hasAlpha() returns true

Tested the particular pixels in the Bitmap before saving and verified that some regions are transparent.

bitmap.compress(Bitmap.CompressFormat.WEBP, 100, fileOutputStream) 

The above code works correctly when tested with API v22, v23. transparency details are stored in the generated file.

The same code, when tested with API v18, v19, v21 doesn't save the alpha channel to file. The transparent regions are black.

All API versions work correctly while decoding the WEBP file.

I'm about to bundle the libwebp with the app, but seems like all documentation suggests that this should be supported. Has anyone faced a similar issue?

UPDATE: I implemented the encoding using libwebp native library. Still curious if anyone else came across the same issue


回答1:


I think its a bug in Android. Documentation suggests that alpha channel for webp is supported starting 4.2.1, but thats probably just the decoding for viewing in Chrome browser & Photo Gallery.

I solved the problem by integrating the libwebp native library and making the call over JNI to encode the Bitmap. Works perfectly!



来源:https://stackoverflow.com/questions/38753798/android-webp-encoding-in-api-v18-and-above-bitmap-compressbitmap-compressforma

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