How to reload image in Glide from the same url? - android

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.

Related

Glide Does not use Cached Image

Hi I am using Glide to load image and after following many posts I tried to implement Glide caching using
RequestOptions().diskCacheStrategy(DiskCacheStrategy.ALL).centerCrop()
Glide.with(mContext)
.load(imageUrl)
.transition(ImageUtil.crossFadeTransition())
.apply(ImageUtil.requestOptionsForSlider())
.listener(glideRequestListner)
.into(imageView)
My knowledge is that if I use diskCacheStrategy(DiskCacheStrategy.ALL) then the above Glide image loading code will automatically use cache if available, or download if not, right?
But I see everytime the code is called, (checking my internet speed meter) that it is always fetching/downloading from the given url (even though I can see the app memory size is increasing).
I even tried this code also-
val future: FutureTarget<File> = Glide.with(mContext)
.load(imageUrl)
.downloadOnly(500, 500)
Nothing seems to work
What should I do so that Glide uses cache if available?What is wrong with my conception about the caching implementation? Any help is very much appreciated!
Are you sure it is downloading again ? did you try disabling internet just before loading the images ?
Otherwise, is your image URL always the same for the same image ? (as Glide uses it as a cache key).
You can use this in the following calls to check if images are in cache:
Glide.with(fragment)
.load(url)
.onlyRetrieveFromCache(true)
.into(imageView);

Glide v4.4 disable memory cache and disk cache failure

The image file is downloaded from the server and always save to the same file name.
So in earlier version of glide, i have been tested with
Uri uri = Uri.fromFile(new File(downloadPath));
GlideApp.with(this)
.load(uri)
.diskCacheStrategy(DiskCacheStrategy.NONE)
.skipMemoryCache(true)
.into(mDownloadImageView);
It seems work for the file with the same name when its image changes. The offical document Glide Caching mentioned like above.
But now in Glide 4.4.0, i used this code again and it is not working. I need to add the signature like below:
Uri uri = Uri.fromFile(new File(downloadPath));
GlideApp.with(this)
.load(uri)
.signature(new ObjectKey(System.currentTimeMillis()))
.diskCacheStrategy(DiskCacheStrategy.NONE)
.skipMemoryCache(true)
.into(mDownloadImageView);
After adding the signature key, then it works.
Does anyone met the same scenario? DiskcacheStrategy None and skip memory cache true as no effect at all to me.
You have to use the RequestOptions now.
Glide.with(this).load(imageResource)
.apply(RequestOptions()
.placeholder(R.drawable.ic_no_profile_image)
.centerCrop()
.diskCacheStrategy(DiskCacheStrategy.NONE)
.skipMemoryCache(true)
.dontAnimate())
.into(profileImageView)
Yes, I have the same problem.
I have to use signature with diskCacheStrategy() set to NONE and skipMemoryCache() set to true if I want to refresh image every time.
Another problem is that if I use diskCacheStrategy() set to ALL with some signature (in order to force cache refresh on some occasions) - it is not working. Signature is changing every time but Glide always loads image from cache and doesn't want to refresh it.
It was working great on Glide v3 and stopped working correctly after upgrading to v4.

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 - use picasso to load image without storing it in cache

I want to use picasso to load an image from a url into a placeholder, but not store that image in cache - in other words, I want the image to be downloaded from the net directly to disk and then loaded from disk when needed. I understand there's a class called RequestCreator where you can specify memory policy - does anyone have an example of using picasso/requestcreator to do something like this?
So.. something like:
RequestCreator requestCreator = new RequestCreator();
requestCreator.memoryPolicy(MemoryPolicy.NO_CACHE);
....
merged with:
Picasso.with(context).load(someurl).fit().placeholder(someplaceholder).into(sometarget)..
Picasso supports this by it's skipMemoryCache() in the Picasso builder. An example is shown below.
Picasso.with(context).load(imageUrl)
.error(R.drawable.error)
.placeholder(R.drawable.placeholder)
.skipMemoryCache()
.into(imageView);
With the new API you should use it like this so that it skips looking for it and storing it in the cache:
Picasso.with(context).load(imageUrl)
.error(R.drawable.error)
.placeholder(R.drawable.placeholder)
.memoryPolicy(MemoryPolicy.NO_CACHE, MemoryPolicy.NO_STORE)
.into(imageView);
NO_CACHE
Skips memory cache lookup when processing a request.
NO_STORE
Skips storing the final result into memory cache. Useful for one-off requests to avoid evicting other bitmaps from the cache.
For picasso:2.71828 or above version use the following for skipping using disk cache networkPolicy(NetworkPolicy.NO_CACHE) :
Picasso.get()
.load(camera_url)
.placeholder(R.drawable.loader2)
.networkPolicy(NetworkPolicy.NO_CACHE, NetworkPolicy.NO_STORE)
.into(img_cam_view);
Picasso 2.5.0
If you are using Picasso to load image from Internet, you have to use NetworkPolicy attribute.
.networkPolicy(NetworkPolicy.NO_STORE)
but live memory cache (Not disk cache) is useful, you might want to keep it.
just append this at the end of url.
"?=" + System.currentTimeMillis();

Captcha image cannot be refresh by using Glide

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

Categories

Resources