I am trying to download a bitmap image in an android app using the Glide library (version 4), but when I do that the transparency of the image is lost and it is replaced by a white background.I tried using the commands:
Glide.with(this).load(url).into(imageView);
Glide.with(this).asBitmap().load(url).into(imageView);
and
Glide.with(this).load(url).apply( new RequestOptions().format(DecodeFormat.PREFER_ARGB_8888).encodeFormat(Bitmap.CompressFormat.PNG)).into(imageView);
However nothing seems to work.
The image url is: https://s151.convertio.me/p/HwxwbAP4nbAyKsEmdn7ORw/1dc5e4a91d3de94ef6c5fcd51b0fb79d/yellow_circle_1.bmp
And here are some screenshots of the app:
This is how it should look.
(It looks like that when I use a png image, the red background is the ImageView's background and the image background is transparent.)
This is how it looks with a bitmap
(The transparent background became white.)
Thanks a lot.
There is no alpha channel in BMP format, as far as I know
https://en.wikipedia.org/wiki/BMP_file_format
Glide.with(context).load(dataModel.getImgurl())
.thumbnail(0.5f)
.fitCenter()
.centerCrop()
.diskCacheStrategy(DiskCacheStrategy.ALL)
.into(imageView);
Related
I am using Glide to get an image as a byte array as such
Glide.with(context)
.as(byte[].class)
.load(url)
.submit()
.get();
and it works well with an image with a background, as well as GIFs. However, when there is a static image with a transparent background (.PNG), it will produce a black background instead of keeping its transparency. I know you can avoid this problem by using a bitmap, but I want to retain both .PNG and .GIF files. Is there a possible way to retain the transparent background when loading a byte array using Glide?
I'm loading an image from a file. It's a photo gallery so I don't want the image resolution to be scaled down. I've found this code snippet that seems to work great on my devices but I've had users complain about low res images before (That I've been unable to replicate but have confirmed they're real issues) so I want to play it safe.
Will this code snippet load the full resolution image or does it require more than this?
File pageFile = getPageFile();
Glide.with(mPhotoView).load(pageFile)
.apply(new RequestOptions()
.override(Target.SIZE_ORIGINAL))
.into(mPhotoView);
For clarity:
Previously, we were creating our own bitmap from the file input stream and applying it like this:
Glide.with(mPhotoView).load(bitmap).into(mImageView);
This is when users started complaining about image resolution
Please try below:-
Glide.with(imageView).load(url)
.apply(new RequestOptions()
.fitCenter()
.format(DecodeFormat.PREFER_ARGB_8888)
.override(Target.SIZE_ORIGINAL))
.into(imageView);
Also, add this android:adjustViewBounds="true" to your ImageView in XML.
You can use centerCrop() instead of fitCenter() to scale. If used centerCrop(), it scales the image so that it fills the requested bounds and then crops the extra.
This is the original image:
This is the rendered image using ImageView:
However, sometimes when the image is in a carousel, swiping back to the image may cause the image to render correctly, which is even more weird...
This behavior is observed both on an LG G3 (Android 5.1) and Genymotion (Android 4.4.4). I'm using the Glide library for loading images, using the ARGB_8888 decode format:
new GlideBuilder(this).setDecodeFormat(DecodeFormat.PREFER_ARGB_8888);
This is a resolved issue 305. Here is a quick recap:
This issue appears only with images with JPEG format (the quality is irrelevant). It looks like it affects RGB_565 much more significantly than ARGB_8888, so you may want to switch the DecodeFormat to ARGB_8888 (clear the app data to check if the issue is resolved). But it can appear even with ARGB_8888, so use one of the following solutions:
Use DiskCacheStrategy.NONE (for local images) or DiskCacheStrategy.SOURCE (for remote images) to prevent Glide from re-compressing the images:
Glide.with(this)
.load(url)
.diskCacheStrategy(DiskCacheStrategy.SOURCE)
.into(imageView);
Use asBitmap() and a custom BitmapEncoder to always compress affected images as PNGs:
Glide.with(this)
.fromResource()
.asBitmap()
.encoder(new BitmapEncoder(Bitmap.CompressFormat.PNG,100))
.load(R.drawable.testimg)
.into(imageView);
Just in case someone tried all that is listed above and none of it worked (like in my case), there is another workaround. Since greenish colour happens during transformation, we can avoid transformation.
Glide.with(context)
.load(url)
.dontTransform()
.into(holder.productImage);
This issue may happen on few devices not all like one plus 3 or 3T and some LG devices when fetching imageUrl from server to your android project.
public static void loadImageWith(Context context, String imageUrl, ImageView imageView) {
Glide.with(context)
.load(imageUrl)
.diskCacheStrategy(DiskCacheStrategy.SOURCE)
.dontTransform()
.placeholder(R.drawable.empty_photo)
.into(imageView);
}
centerCrop() may create issue, so avoid to use centerCrop().
I am downloading low resolution pictures from web by using Picasso and displaying it in high res displays. Fetching images is straightforward:
Picasso.with(context)
.load(item.getPicture())
.transform(new BitmapTransformations.OverlayTransformation(
context.getResources(), R.drawable.ic_play_video))
.error(R.drawable.picture_placeholder)
.into(target);
Notice that I apply Overlay transformation into request - It's a high quality image asset I put on top of downloaded image. Applying overlay is also simple - just resize bitmap and put it in the center of image. But here comes the problem: Unless I add resizing, in my case:
.fit().centerCrop();
into Picasso request, overlaid high-res asset is copying quality of downloaded image (first picture). When I add this resizing before transformation the overlay looks as expected (second picture). Can anyone explain to me why this happens? Thanks you!
I'm displaying an external image in an image view by fist downloading it like this:
bitmap = BitmapFactory.decodeStream((InputStream)new URL(url).getContent());
Then setting this bitmap to the ImageView like this:
imageView.setImageBitmap(bitmap);
This works all good except that one of the images is a PNG and I lose the transparent background when using the BitmapFactory.
Can anyone tell me how I can keep the transparent background?
not sure if this will help, but try following this advice and adding Options to make sure that your image is pulled in as ARGB_8888
http://developer.android.com/reference/android/graphics/BitmapFactory.html#decodeFile(java.lang.String, android.graphics.BitmapFactory.Options)