3

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!

ryuujin
  • 192
  • 3
  • 16
  • 1
    Check the WebP itself, using some tool on your development machine. Does the WebP that you saved have the alpha channel? In other words, is the problem in the saving of the image, or in the decoding of the image? – CommonsWare Sep 10 '16 at 17:40
  • 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. – ryuujin Sep 10 '16 at 17:54
  • now I'm trying to pull a WEBP image with transparency from my computer to emulator... I want to see how it will be handled. – ryuujin Sep 10 '16 at 18:11
  • I can confirm the problem is how the file is saved. – ryuujin Sep 10 '16 at 18:18
  • 1
    I don't see any bugs related to this on http://b.android.com -- most of the complaints about WebP seem to be in displaying images, not encoding them. Then again, encoding WebP with transparency may not be all that common. Unfortunately, I don't have any suggestions, other than seeing if there is some third-party library that you can use. – CommonsWare Sep 10 '16 at 18:21
  • Thanks! I'm continuing the investigation. I've found another question in stackoverflow with the same problem but without answer: http://stackoverflow.com/questions/38753798/android-webp-encoding-in-api-v18-and-above-bitmap-compressbitmap-compressforma :( – ryuujin Sep 10 '16 at 18:23
  • OTOH, that question points out that `libwebp` apparently is an option, so that's a plus. – CommonsWare Sep 10 '16 at 18:26
  • Any update on that to resolve? – Lakindu Akash Oct 17 '19 at 08:31

0 Answers0