glide icon indicating copy to clipboard operation
glide copied to clipboard

Incorrect background on Lollipop for a GIF displayed asBitmap()

Open lynfogeek opened this issue 11 years ago • 15 comments

Let's say I want to load a static version of this GIF into an ImageView:

miaow

I am currently using this:

Glide.with(getContext())
            .load(image.getUrl())
            .asBitmap()
            .diskCacheStrategy(DiskCacheStrategy.SOURCE)
            .placeholder(R.drawable.ic_placeholder)
            .into(imageView);

As a result, the image is properly loaded on a device / emulator pre-lollipop (with transparent background) but it will be drawn with a white background on Lollipop.

Some other gifs will even be displayed with a purple, blue or black background. Like this one: image

note: If i load them .asGif() the background will be transparent as expected

Is there any workaround to get a transparent background on Lollipop?

lynfogeek avatar Apr 18 '15 12:04 lynfogeek

There's a couple of potential causes here. Images with black backgrounds may be caused by an incorrect Bitmap configuration, you can test by either switching to ARGB_8888 globally, or by using the format() method.

Another potential problem is that when we re-use Bitmaps, we're somehow failing to clear out the background of the image. You can test this by passing in BitmapPoolAdapter to the GlideBuilder class Glide uses for configuration to temporarily disable Bitmap re-use and see if that fixes the problem.

Finally, it's also possible that Android's BitmapFactory class is decoding some of these images with backgrounds whereas our GifDecoder class is not. asGif() will force the image to be decoded using our GifDecoder, asBitmap() will force the image to be decoded as a single frame using BitmapFactory, and using neither asBitmap() nor asGif() will decode GIFs with more than one frame using our GifDecoder and single frame GIFs with BitmapFactory.

tldr: try forcing ARGB_8888, then try disabling Bitmap reuse via BitmapPoolAdapter, then try just decoding the images with BitmapFactory#decodeStream directly.

If you can let me know what happens after each of those steps and/or post urls to the original images so I can (eventually) get to testing them myself, I'd bet we can make progress. Thanks for the report!

sjudd avatar Apr 19 '15 18:04 sjudd

Thank you @sjudd for you help. I tried these different solutions.

  • Forcing ARGB_8888 via format() or globally doesn't not help
  • Disabling the Bitmap reuse does not seem to help. I also tried to only load a single image, so the library should not need to reuse the bitmap.

I am not sure to understand how can I decode it with BitmapFactory#decodeStream. But I assume the problem comes from the Android BitmapFactory because my problem only happen on Lollipop and works fine on KitKat / Jelly Bean.

Example of urls: White background instead of transparent Blue background instead of transparent

lynfogeek avatar Apr 25 '15 10:04 lynfogeek

@lynfogeek what he meant is to skip Glide and just manually load the image in onCreate for example:

imageView.setImageBitmap(BitmapFactory.decodeStream(...));

If you have wrong backgrounds this way, then it's a framework/gif file bug.

TWiStErRob avatar Apr 25 '15 10:04 TWiStErRob

When you say Lollipop, is it 5.0, 5.0.x or 5.1?

TWiStErRob avatar Apr 25 '15 10:04 TWiStErRob

@TWiStErRob thank you. The background is wrong using BitmapFactory#decodeStream on Android Kitkat and Lollipop but it works fine with older versions.

Tested with more versions to be more precise:

  • :white_check_mark: 4.1.1
  • :white_check_mark: 4.3
  • :x: 4.4.4
  • :x: 5.0
  • :x: 5.1

lynfogeek avatar Apr 25 '15 14:04 lynfogeek

@lynfogeek It does indeed look like an issue with BitmapFactory. If you can attach a minimal sample app that you used to test those various platform versions, I'll do my best to pass it along.

For now, you can use asGif() as a workaround. If you don't want the images to be animated, you can extend GlideDrawableImageViewTarget, override onResourceReady(), and always call stop() on the drawable after calling super. You could also look in to using a ResourceTranscoder to transform the GifDrawable into a simple Bitmap.

sjudd avatar Apr 25 '15 16:04 sjudd

Actually don't worry about the sample app, I'll look in to it.

sjudd avatar Apr 25 '15 16:04 sjudd

Internal bug is b/20567643

sjudd avatar Apr 25 '15 17:04 sjudd

I'm going to close this for now since I don't think we want to go to the effort (and take the performance hit) of working around the issue.

If it seriously affects you, you can work around the problem yourself using asGif() to force the image to be decoded with Glide's GifDecoder.

sjudd avatar Apr 28 '15 17:04 sjudd

i got the same issue in SDK v3.6.0

bearprada avatar Aug 03 '15 04:08 bearprada

Try working around it by https://github.com/bumptech/glide/issues/423#issuecomment-96232571 (second paragraph).

TWiStErRob avatar Aug 03 '15 10:08 TWiStErRob

I HAVE THE SAME ISSUE. Glide load image,There is a blue background.

tjych avatar Nov 01 '15 13:11 tjych

@tjych Did you try second paragraph of https://github.com/bumptech/glide/issues/423#issuecomment-96232571?

TWiStErRob avatar Nov 01 '15 14:11 TWiStErRob

I load image , not gif , There is a blue background, My English is not good .

tjych avatar Nov 02 '15 05:11 tjych

If the format is PNG the black background won't be displayed.

anupamchugh avatar Sep 06 '18 06:09 anupamchugh