(Android) How to download an image/gif without a black background? - android

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?

Related

Remove transparent white background from gif image using glide?

How can I remove white background around image when loading with glide.. You can find out below code as well as screenshot;
Glide.with(thiscontext).load(R.drawable.badge_green_yellow)
.asGif().crossFade()
.diskCacheStrategy(DiskCacheStrategy.SOURCE).into(holder.ivHourlyIcon);
ScreenShot
Put your GIF into the raw folder (res/raw/badge_green_yellow.gif)
and then load it like this
Glide.with(thiscontext).asGif().load(R.raw.badge_green_yellow).into(holder.ivHourlyIcon);

Bitmaps lose transparency when downloaded with Glide

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);

Loading bitmap to ImageView using Async

I am loading a bitmap asynchronous (ASyncTask) to the ImageView (its a background picture). The user can pick a custom background picture. When I start the application, I am loading the bitmap to the ImageView, but that takes some time (like 0.3 seconds). In this 0.3 seconds, I see the default background (black screen). I do like to see the custom background immediately.
How can I achieve this?
EDIT
With Glide I get exactly the same result, again a black screen for half a second.
If I use this code, without async, it works, but is good practise?
inputStream = new FileInputStream(createFile());
return BitmapFactory.decodeStream(inputStream);
Take a look at Picasso Library
http://square.github.io/picasso/
It works like this :
Picasso.with(context).load("http://i.imgur.com/DvpvklR.png").into(imageView);
With internal storage bitmap :
Picasso.with(context).load(new File(path)).into(imageView);
You can add an image that will be displayed during loading :
.placeholder(getResources().getDrawable(R.drawable.default_image))

RenderScript blur breaks bitmap loaded by Glide

So, the thing is that I am fetching some images using Glide. I fetch them directly into bitmap, and then I blur that bitmap using RenderScript and than show on UI blurred one.
UI itself has "All Images" activity and "Single Image" activity. User clicks on the image on the first activity, and the blurred version is showed on the second one, so it is possible to go back and forth opening and closing the same image.
The issue is that this cause image to become broken, and there is no way to fix it unless you clear all app data.
The issue even survives app reinstall (using Android Studio). So, if I open image, and it is displayed as it should, than make some changes in code, and install the app again, image would show broken immediately after
installation, unless I clear data.
It happens only with bitmaps loaded using glide. If I get some drawable resource as bitmap, everything works well.
UPDATE:
This is the code used here;
Bitmap logo = Glide.with(context)
.load(url)
.asBitmap()
.into(80, 80)
.get();
return BlurBuilder.blur(context, logo);
And BlurBuilder is the class copied from here: Create blurry transparent background effect
Had the same problem. Solved it by setting the Glide decode format to ARGB_8888
https://github.com/bumptech/glide/wiki/Configuration#bitmap-format

Android Glide: Show a blurred image before loading actual image

I am developing an Android app which displays full screen images to the user. Images are fetched from the server. I am using Glide to show the image. But I want to display a very small size blurred image before displaying the actual image. Once the image is cached, directly full sized image should be shown.
Image Displaying flows goes like this:
- If image is downloaded for the first time, first download a small scaled image, and then download full resolution image.
- If image has been downloaded before, directly show full scale image.
I cannot find any method in Glide library, which tell me if a file exist in cache or not.
Any idea, how this can be done.
Glide.with(context.getApplicationContext())
.load(Your Path) //passing your url to load image.
.override(18, 18) //just set override like this
.error(R.drawable.placeholder)
.listener(glide_callback)
.animate(R.anim.rotate_backward)
.centerCrop()
.into(image.score);
with Glide v4:
Glide.with(context)
.load(URL)
.thumbnail(0.1f)
.into(image_view)
Better you can get idea from this Library or Use this library.
2 ways to do Blur and Original image in Android.
1) Initially load Blur Image Server URL path and once get success bitmap caching mechanism to load Original(Large) Image Server URL path.
Point 1 is possible, I got answer for you( Ask your server team to get Blur image or Low quality image to make blur and then load large image).
APK Link
Glide.with(mContext)
.load(image.getMedium())
.asBitmap()
.diskCacheStrategy(DiskCacheStrategy.ALL)
.into(new SimpleTarget<Bitmap>() {
#Override
public void onResourceReady(Bitmap bitmap, GlideAnimation anim) {
// Do something with bitmap here.
holder.thumbnail.setImageBitmap(bitmap);
Log.e("GalleryAdapter","Glide getMedium ");
Glide.with(mContext)
.load(image.getLarge())
.asBitmap()
.diskCacheStrategy(DiskCacheStrategy.ALL)
.into(new SimpleTarget<Bitmap>() {
#Override
public void onResourceReady(Bitmap bitmap, GlideAnimation anim) {
// Do something with bitmap here.
holder.thumbnail.setImageBitmap(bitmap);
Log.e("GalleryAdapter","Glide getLarge ");
}
});
}
});
2) Fresco by Facebook is a new image library for Android. Here’s the official blog post introducing the library. It supports the streaming of progressive JPEG images over the network which can be really helpful to display large images over slow connections. One can see the image gradually improve in quality.
Progressive JPEGs with Facebook Fresco
Use BitmapFactory.Options.inSampleSize to make a downsampled version of the image, and upload both versions. Load the sample image (which should take less time) and when the bigger version is downloaded, switch to that. You could also use a TransitionDrawable to make a fade transition.
this is not a perfect way but it will be easiest way to make it.
make placeHolder image as blur and set it into glide. then always it will show blur image and then after load the actual image it will appear.
you can refer this code to use glide.
//crete this method into your Utils class and call this method wherever you want to use.
//you can set these placeHolder() and error() image static as well. I made it as comment inside this method, then no need to use [placeHolderUrl and errorImageUrl] parameters. remove it from this method.
public static void loadImage(final Activity context, ImageView imageView, String url, int placeHolderUrl, int errorImageUrl) {
if (context == null || context.isDestroyed()) return;
//placeHolderUrl=R.drawable.ic_user;
//errorImageUrl=R.drawable.ic_error;
Glide.with(context) //passing context
.load(getFullUrl(url)) //passing your url to load image.
.placeholder(placeHolderUrl) //this would be your default image (like default profile or logo etc). it would be loaded at initial time and it will replace with your loaded image once glide successfully load image using url.
.error(errorImageUrl)//in case of any glide exception or not able to download then this image will be appear . if you won't mention this error() then nothing to worry placeHolder image would be remain as it is.
.diskCacheStrategy(DiskCacheStrategy.ALL) //using to load into cache then second time it will load fast.
.animate(R.anim.fade_in) // when image (url) will be loaded by glide then this face in animation help to replace url image in the place of placeHolder (default) image.
.fitCenter()//this method help to fit image into center of your ImageView
.into(imageView); //pass imageView reference to appear the image.
}
glide has a direct way of doing this :
val thumbnailRequest = Glide.with(this)
.load("https://picsum.photos/50/50?image=0")
Glide.with(this)
.load("https://picsum.photos/2000/2000?image=0")
.thumbnail(thumbnailRequest)
.into(imageThumbnail)
from the blog:
Thumbnails are a dynamic placeholders, that can be loaded from the Internet. Fetched thumbnail will be displayed until the actual request is loaded and processed. If the thumbnail for some reason arrives after the original image, it will be dismissed.
You must have a small-size and a full-size image on your server. You can use Firebase Resize Image Extension if you are using Firebase as your backend. Once you have a small-size image URL and a full-size image URL you can use Glide like this:
Glide.with(context)
.load(fullSizeImageUrl)
.thumbnail(
Glide.with(context)
.load(smallSizeImageUrl)
).into(imageView)
You'll get that blurry effect as the small-size image will be blurry
According to Glide Doc:
.thumbnail()
loads and displays the resource retrieved by the given thumbnail request if it finishes before this request. Best used for loading thumbnail resources that are smaller and will be loaded more quickly than the full size resource. There are no guarantees about the order in which the requests will actually finish. However, if the thumb request completes after the full request, the thumb resource will never replace the full resource.
use BitmapFactory.Options.inSamleSize to load a downsampled version of the image. Then load the bigger image and do a fade transition using a TransitionDrawable
Hope this will Helps you ! Cheers !

Categories

Resources