Load GIF From URL - android

I tried to get this GIF from a URL like this:
Glide.with(context)
.load("http://artfcity.com/wp-content/uploads/2017/07/tumblr_osmx1ogeOD1r2geqjo1_540.gif")
.into(new GlideDrawableImageViewTarget(imageViewBaby) {
#Override
public void onResourceReady(GlideDrawable drawable, GlideAnimation anim) {
super.onResourceReady(drawable, anim);
imageViewBaby.setImageDrawable(drawable);
imageViewBaby.invalidate();
imageViewBaby.requestLayout();
}
});
But the GIF will not move or start. It's just like an image! How to solve this issue?

Try this
Add dependency
implementation 'com.github.bumptech.glide:glide:4.4.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.4.0'
Gif Loading
Glide.with(this)
.load("http://artfcity.com/wp-
content/uploads/2017/07/tumblr_osmx1ogeOD1r2geqjo1_540.gif")
.apply(RequestOptions.diskCacheStrategyOf(DiskCacheStrategy.NONE))
.into(imageView);
See this https://github.com/bumptech/glide/issues/2471

As android doesnot support gif files, you can use webview to display the gif file.

Please use .asGif() to load gif images inside your ImageView:
Glide.with(context)
.load(gifUrl)
.asGif()
.diskCacheStrategy(DiskCacheStrategy.SOURCE).override(200, 200)
.into(((GifViewHolder) holder).gifImageView);

Related

Image flickering when Glide loads it

I have a list of image views and some of them will be frequently updated with a fixed url. By appending a timestamp query parameter in the end of the url. It works, but I found when it updates it will also clear the current content. Any way to prevent this?
// the one needed to update with timestamp appended
image += "?"+String.valueOf(System.currentTimeMillis());
Glide.with(mContext.getApplicationContext())
.load(image)
.error(R.drawable.default_avatar)
.centerCrop()
.crossFade()
.into(((VideoViewHolder) holder).img);
// the others don't need to update
Glide.with(mContext.getApplicationContext())
.load(image)
.error(R.drawable.default_avatar)
.centerCrop()
.crossFade()
.into(((VideoViewHolder) holder).img);
Note that the others without timestamp appended are all good.
Try this
Glide
.with(context)
.load(filepath)
.asBitmap()
.diskCacheStrategy(DiskCacheStrategy.NONE)
.skipMemoryCache(true)
.dontAnimate()
.into(new SimpleTarget<Bitmap>() {
#Override
public void onResourceReady(Bitmap arg0, GlideAnimation<? super Bitmap> arg1) {
// TODO Auto-generated method stub
holder.mItemView.setImageBitmap(arg0);
}
});
If you're using Glide 3.x and wish to directly display the image without the small crossfade effect, call .dontAnimate() on the Glide request builder:
for more read this : Glide — Placeholders & Fade Animations
Happy coding!!
If you are using Emulator, then this can happens.
Try using a real physical device or change the emulator.

Glide gif doesnt play

I want to load a gif into an imageview, it is displayed but doesn't start playing. I use this code:
Glide.with(context)
.load("https://media.giphy.com/media/7rj2ZgttvgomY/giphy.gif")
.into(imageView)
I've also tried adding .asGif() but in that case image is not displayed at all. I'm using glide 3.8.0
try this.
Glide.with(context)
.load(imageUrl)
.asGif()
.placeholder(R.drawable.loading2)
.crossFade()
.into(imageView);
In my case I had set the imageview dimensions to match_parent and specifying it in dp did it for me.
Not sure why exactly match_parent doesn't work in a dialog, playing GIFs, probably something to do with the scaling, if this is the case then using scaleType:fitXY should work too
You can also try
Glide
.with(context)
.load(gifUrl)
.asGif()
.error(R.drawable.full_cake)
.diskCacheStrategy(DiskCacheStrategy.SOURCE)`
.into(imageViewGif);
Change your gif your to https://i.giphy.com/media/7rj2ZgttvgomY/giphy.webp
Glide.with(context)
.load("https://i.giphy.com/media/7rj2ZgttvgomY/giphy.webp")
.asGif().into(imageView);
Also you can try GIFView
You can refer to : https://github.com/bumptech/glide/issues/1059
If you don't find it, you can change the lastest version of Gline
repositories {
mavenCentral()
maven { url 'https://maven.google.com' }
}
dependencies {
compile 'com.github.bumptech.glide:glide:4.2.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.2.0'
}
I hope you can find solution.
There is nothing wrong with your code. You could check if you have Internet permission.
<uses-permission android:name="android.permission.INTERNET" />
Your internet speed might be slow and Glide might be taking too much time to load so by that time you could add a placeholder image using :
.placeholder(android.R.color.holo_green_dark)
Or you could add Listner to the Request using
.listener(new RequestListener<String, GlideDrawable>() {
#Override
public boolean onException(Exception e, String model, Target<GlideDrawable> target, boolean isFirstResource) {
return false;
}
#Override
public boolean onResourceReady(GlideDrawable resource, String model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {
return false;
}
})
And see what is the Exception.

Android - Glide in the recyclerView

I am new in android developing and i have an issue. I try to load some images in a RecyclerView. The incoming data have the type byte[].
My first solution is converting byte[] to Bitmap and it works fine but the bitmaps stucks in the memory and i was getting an OutOfMemory exception.
The second solution is using Glide.
Glide.with(mContext)
.load(field.getImage())
.into(holder.mImageView);
But with this solution i get same images in each item.
I think the problem is in the Glide's cache because incoming arrays are different. I try to do solve this with this code:
requestOptions.skipMemoryCache(true);
requestOptions.diskCacheStrategy(DiskCacheStrategy.NONE);
Glide.with(mContext)
.applyDefaultRequestOptions(requestOptions)
.load(field.getImage())
.into(holder.mImageView);
}
But it doesn't help. What is the right way to do this?
try this use setDefaultRequestOptions(requestOptions)
Glide.with(context)
.setDefaultRequestOptions(new RequestOptions().placeholder(R.drawable.booked_circle).error(R.drawable.booked_circle))
.load(url)
.into(imageView);
you can resize image like this
Glide.with(PhotoGalleryActivity.this)
.using(new FirebaseImageLoader())
.load(storageReference)
.asBitmap()
.into(new SimpleTarget<Bitmap>(520, 520) {
#Override
public void onResourceReady(Bitmap resource, GlideAnimation glideAnimation) {
holder.img.setImageBitmap(resource);
}
});
For resizing image and adding (placeholder and error image) use this code:
RequestOptions requestOptions = new RequestOptions();
requestOptions.placeholder(R.drawable.app_icon);
requestOptions.error(R.drawable.ic_image_error);
Glide.with(context)
.setDefaultRequestOptions(requestOptions)
.asBitmap()
.load(newsContentModel.getImgNewsUrl())
.into(new SimpleTarget<Bitmap>() {
#Override
public void onResourceReady(Bitmap bitmap, Transition<? super Bitmap> transition) {
// requestLayout()
// Call this when something has changed which has
// invalidated the layout of this view.
myViewHolder.imgImage.requestLayout();
myViewHolder.imgImage.getLayoutParams().height = newHeight;
myViewHolder.imgImage.getLayoutParams().width = newWidth;
// Set the scale type for ImageView image scaling
myViewHolder.imgNewsImage.setScaleType(ImageView.ScaleType.FIT_XY);
myViewHolder.imgImage.setImageBitmap(bitmap);
}
});

Load image synchronously with glide and cache it in the disk

I'm doing something like this to get bitmap synchronously and set it as image by using imageView.setImageBitmap(bitmap);
return Glide
.with(mContext)
.load(url)
.asBitmap()
.into(width, height)
.get();
I need to do it synchronously because I'm doing it inside AsyncTask's doInBackground. I need to fetch image url from the backend before loading it with glide. 
Later I'm loading the same url into view directly with glide.
The problem is that image is not cached in the disk. 
Any solution for this?
Use diskCacheStrategy
return Glide
.with(mContext)
.load(url)
.asBitmap()
.diskCacheStrategy(DiskCacheStrategy.ALL)
.into(width, height)
.get();
add diskCacheStrategy to your Glide, Select StrategyType as Source
You can have idea why to use SOURCE from this link
DiskCacheStrategy.NONE caches nothing, as discussed
DiskCacheStrategy.SOURCE caches only the original full-resolution image.
DiskCacheStrategy.RESULT caches only the final image, after reducing the resolution (and possibly transformations) (default behavior)
DiskCacheStrategy.ALL caches all versions of the image
try:
to get Bitmap From URL:
return Glide
.with(mContext)
.load(url)
.asBitmap()
.diskCacheStrategy(DiskCacheStrategy.SOURCE)
.into(width, height)
.get();
For loading in ImageView :
Glide
.with(mContext)
.load(url)
.diskCacheStrategy(DiskCacheStrategy.SOURCE)
.into(imageView) // your ImageViewID here
For loading Image into ImageView after getting Bitmap:
Glide.with(context)
.load(url)
.asBitmap()
.diskCacheStrategy(DiskCacheStrategy.SOURCE)
.into(new SimpleTarget<Bitmap>() {
#Override
public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
iv_image.setImageBitmap(resource); // you can store resource to some variable too if you want to update more than 1 images or try it like iv_image2.setImageBitmap(resource)
iv_image.requestLayout();
}
});
I have faced this issue many times the best way to overcome it is by using interfaces to load data after it is fetched inside glide CustomTarget
Create a new file called OnglideLoaded.java file like this:
import android.graphics.drawable.Drawable;
public interface OnGlideLoaded {
void onGlideLoaded(Drawable drawable);
}
Now create setter method for it like this where glide is:
public void setOnGlideLoaded(OnGlideLoaded onGlideLoaded) {
this.onGlideLoaded = onGlideLoaded;
}
Not call it inside Glide Target:
Glide.with(context)
.asBitmap()
.load(uri)
.into(new CustomTarget<Bitmap>() {
#Override
public void onResourceReady(#NonNull Bitmap resource, #Nullable Transition<? super Bitmap> transition) {
icon = resource;
if (onGlideLoaded!=null){
//This will send the image to every registered interface
onGlideLoaded.onGlideLoaded(icon);
}
}
#Override
public void onLoadCleared(#Nullable Drawable placeholder) {
}
});
Now call the interface like this where you want the image:
reference.setOnGlideLoaded(new OnGlideLoaded() {
#Override
public void onGlideLoaded(Drawable drawable) {
imageView.setImageBitmap(drawable);
}
});
Here, reference will be the object where you created the setter method.

Glide : How to get the error bitmap?

By using the Glide image loader library, you are able to be notified if the began loading has succeeded.
Glide
.with(this)
.load(uri)
.asBitmap()
.listener(mRequestListener)
.into(imageView);
It works well, because you can get the bitmap loaded by Glide on the onResourceReady callback method of the RequestListener class. Now use an error placeholder:
Glide
.with(this)
.load(uri)
.asBitmap()
.listener(mRequestListener)
.error(R.drawable.error_image)
.into(imageView);
What if I want to get the error image?
When the given image couldn't be found and my error image loads, only the onException callback method gets called and hence this, we can't get the error bitmap.
Are you looking for something like this:
Glide.with(context)
.load(uri)
.asBitmap()
.into(new SimpleTarget<Bitmap>(width, height) {
#Override
public void onResourceReady(Bitmap resource, GlideAnimation glideAnimation) {
// add image to the imageView here
}
#Override
public void onLoadFailed(Exception e, Drawable errorDrawable) {
// you are given the error drawable
}
});

Categories

Resources