Captcha image cannot be refresh by using Glide - android

I have tried to use Glide to load an Captcha image into a ImageView. The first time loading is fine. However, when I reload the Captcha image into the same ImageView, the ImageView does not refresh to new image. Does anyone have idea how to solve this issue?
String url = "https://captcha_path";
ImageView imgView = (ImageView)getActivity().findViewById(R.id.imgView);
Glide.with(getActivity()).load(url).asBitmap().diskCacheStrategy(DiskCacheStrategy.NONE).into(imgView);

You can always use Glide.clear() and then call Glide.with(...).load() again. If your url doesn't change when the image changes, you may also need to add .skipMemoryCache(true) to your load call. For more control, check out the .signature() API. You can always do something like:
Glide.with(fragment)
.load(url)
.signature(new StringSignature(UUID.randomUUID().toString()))
.into(imgView);

Glide.with(fragment)
.load(url)
.signature(new StringSignature(UUID.randomUUID().toString()))
.into(imgView);
Replace StringSignature with ObjectKey (for Glide v4)
Glide.with(fragment)
.load(url)
.signature(new ObjectKey(UUID.randomUUID().toString()))
.into(imgView);

Related

How to load gif image url with Glide?

I can load a gif image url, but not able to load with Glide using this code.
Glide.with(context)
.load("gif imageurl")
.asGif()
.crossFade()
.into(imageview);
How to load a gif image from server using Glide?
You can try this approach:
Glide.with(context)
.load(R.raw.sample_gif)
.into(new GlideDrawableImageViewTarget(yourImageView))
or so, try to move the call of the method asGif before the loading
Glide.with(context)
.asGif()
.load("gif image url")
.crossFade()
.into(imageview);
You can try this below code:
ImageView gif = (ImageView) findViewById(R.id.gif);
Glide.with(this)
.asGif()
.load("gif image url")
.into(gif);
You can try on this way it's work fine.
ImageView im_gif = (ImageView) findViewById(R.id.im_gif);
Glide.with(this).asGif().load(R.raw.gif_logo).into(im_gif);
You can add your gif image file to resource/raw folder.and set that name into load method.

How to remove a particular image URL from disk cache in Glide?

I am loading an image from remote URL into an ImageView with Glide. When I update the image, the content within that URL changes, but the URL location remains same. (This is as expected).
for eg. https://www.the-android-app.com/userid/121.jpg
Now I am showing image into ImageView using this code.
Glide
.with(this)
.load(profilePicUrl) //this is in String format
.diskCacheStrategy(DiskCacheStrategy.RESULT)
.skipMemoryCache(true)
.error(R.drawable.default_profile_pic)
.into(imgProfilePic);
When I update my profile pic, Glide is loading the same old pic from Cache. I need to remove cache entry for this URL before fetching the new image.
Edit:
The url always remains same, only the content inside it changes.
I'm using Glide 3.8.0
You can try signature(new ObjectKey(System.currentTimeMillis()))
Look in Custom cache invalidation for more information.
Example
GlideApp.with(this)
.load(imageUrl)
.signature(new ObjectKey(System.currentTimeMillis()))
.into(imageView);

How to reload image in Glide from the same url?

I am using Glide image loader to load an image from a specific URL, now if I update the image to the same URL, Glide is still showing the cached image in my imageview. How to reload the image from the same URL?
As per the Glide wiki Caching-and-Cache-Invalidation
Option 1 (Glide v4): Use ObjectKey to change the file's date modified time.
Glide.with(requireContext())
.load(myUrl)
.signature(ObjectKey(System.currentTimeMillis().toString()))
.into(myImageView)
Option 1 (Glide v3): Use StringSignature to change the file's date modified time.
URLs - Although the best way to invalidate URLs is to make sure the server changes the URL and updates the client when the content at the URL changes, you can also use StringSignature to mix in arbitrary metadata
Glide.with(yourFragment)
.load(url)
.signature(new StringSignature(String.valueOf(System.currentTimeMillis()))
.into(yourImageView);
If all else fails and you can neither change your identifier nor keep track of any reasonable version metadata,
Option 2: You can also disable disk caching entirely using diskCacheStrategy() and DiskCacheStrategy.NONE
Glide.with(Activity.this).load(url)
.diskCacheStrategy(DiskCacheStrategy.NONE )
.skipMemoryCache(true)
.into(imageView);
Reference: https://github.com/bumptech/glide/wiki/Caching-and-Cache-Invalidation
Use .diskCacheStrategy(DiskCacheStrategy.NONE)
Glide.with(context)
.load(url)
.apply(new RequestOptions()
.placeholder(R.drawable.placeholder)
.error(R.drawable.error)
.diskCacheStrategy(DiskCacheStrategy.NONE)
.skipMemoryCache(true))
.into(ImageView);
Add
signature(new ObjectKey(String.valueOf(System.currentTimeMillis())))
RequestOptions requestOptions = new RequestOptions();
requestOptions.placeholder(R.drawable.cover_placeholder);
requestOptions.error(R.drawable.no_image_available);
requestOptions.signature(
new ObjectKey(String.valueOf(System.currentTimeMillis())));
Glide.with(MosaicFragment.this)
.setDefaultRequestOptions(requestOptions)
.load(finalPathOrUrl)
Use below code, it working fine for me. Set diskCacheStrategy(DiskCacheStrategy.NONE) and skipMemoryCache(true). It will load the image every time.
Glide.with(Activity.this)
.load(theImagePath)
.diskCacheStrategy(DiskCacheStrategy.NONE)
.skipMemoryCache(true)
.into(myImageViewPhoto);
After struggling for a couple of hours, I find the solution.You can mix in the datetime of file by adding StringSignature.So when the file will load,It will always use the latest one.Like this
Glide.with(this)
.load(image_url)
.signature(new StringSignature(String.valueOf(System.currentTimeMillis())))
.into(imageview);
Reference : https://github.com/bumptech/glide/wiki/Caching-and-Cache-Invalidation
I used a work around since no other method worked for me
In my case I knew that when the image on the server will change so I was just adding ?= at the end of the url, for glide it will be a new url and instead of using cache Glide will load the new image.

cache image without showing glide

Is it possible to cache image using glide without showing it in the imageView?. If it is then how?.
Right now I'm doing this code:
Glide
.with(getApplicationContext())
.load("imageUrl")
.override(windowWidth(),(int)windowWidth()*0.5))
.diskCacheStrategy(DiskCacheStrategy.ALL);
But this is not working , when app is open glide load image not from cache but from url.
Since glide 4.X:
//to save img
RequestManager rm = Glide.with(context);
rm.load(imgUrl).submit();
//to load img
Glide.with(context)
.applyDefaultRequestOptions(
new RequestOptions().diskCacheStrategy(DiskCacheStrategy.ALL))
.load(imgUrl)
.into(view);
Based on this GitHub topic
i've never used it, but referring the documentation: have you tried the downloadOnly?
Glide's downloadOnly() API allows you to download the bytes of an image into the disk cache so that it will be available to be retrieved later.
https://github.com/bumptech/glide/wiki/Loading-and-Caching-on-Background-Threads#downloadonly
To preload remote images and ensure that the image is only downloaded once:
Glide.with(context)
.load(yourUrl)
.diskCacheStrategy(DiskCacheStrategy.SOURCE)
.preload();

Android Glide failing to load image

I am trying to load an image into an ImageView using the Glide library. The URL that I pass into the load() method points to an endpoint ('/api/user/5/logo'), which then returns the image. For some reason this doesn't seem to work.
However if I pass a URL that points directly to an image ('www.example.com/logo.png'), it works perfectly.
How do I get Glide to work perfectly with the required web service?
To recap, this works:
Glide.with(getContext())
.load("www.example.com/content/logo.png")
.centerCrop()
.placeholder(R.drawable.mark)
.into(imageView);
.. and this doesn't:
Glide.with(getContext())
.load("www.example.com/api/user/5/logo")
.centerCrop()
.placeholder(R.drawable.mark)
.into(imageView);
Any help would be greatly appreciated.
Try with this:
Glide.with(getContext())
.load("http://www.example.com/api/user/5/logo").centerCrop().into(imageview)

Categories

Resources