问题
I wrote a service that retrieves image from web (JPEG, PNG, ...), which I then, save to disk in webp format.
I save the image using the following code:
try (FileOutputStream fos = new FileOutputStream(imgFile)) {
bitmap.compress(Bitmap.CompressFormat.WEBP, 100, fos);
} catch (IOException e) {
Log.e(TAG, "IOException writing file");
} catch (SecurityException e) {
Log.e(TAG, "SecurityException writing file");
}
I have not got any warnings.
If the App runs in a device with API 22 or 23 the decoded image files are shown with the alpha channel when is present.
If I run the app in an API19 Emulator the decoded image is not shown with the alpha channel (it is shown black).
I preferred WEBP because is lighter than PNG and it should have alpha channel.
PS: I tried with quality 80 and 100 too
Comment 1
The retrieved image is a PNG with alpha channel.
The same image in other devices (and emulators too) with API > 22 when converted in WEBP it shows the transparency.
I tried with other PNG's too. If I use the retrieved bitmap and I save it in PNG in all devices it is shown correctly with transparency, in API 19 too.
The issue is verified only when I save it in webp format. I tried to pull the webp encoded file from the emulator API 19 and it hasn't the alpha channel anymore.
I retrieve the bitmap in this way:
try (InputStream is = new URL(mImageUrl).openStream()) {
bitmap = BitmapFactory.decodeStream(is);
} catch (MalformedURLException e) {
Log.e(TAG, "MalformedURLException " + e.getMessage());
} catch (IOException e) {
Log.e(TAG, "IOException decoding url " + e.getMessage());
}
COMMENT 2
If I tried to replace the WEBP file in the emulator with one with transparency from my computer, then... the App will show the image with transparency.
So, it is a problem of how the file is saved!
来源:https://stackoverflow.com/questions/39428789/bitmap-compressformat-webp-on-android-api-19-alpha-channel-becomes-black