Glide not updating image android of same url?
Solution 1:
//Use bellow code, it work for me.Set skip Memory Cache to true. it will load the image every time.
Glide.with(Activity.this)
.load(theImagePath)
.diskCacheStrategy(DiskCacheStrategy.NONE)
.skipMemoryCache(true)
.into(myImageViewPhoto);
Solution 2:
Glide 4.x
Glide.with(context)
.load(imageUrl)
.apply(RequestOptions.skipMemoryCacheOf(true))
.apply(RequestOptions.diskCacheStrategyOf(DiskCacheStrategy.NONE))
.into(imageView);
Glide 3.x
Glide.with(context)
.load(imageUrl)
.skipMemoryCache(true)
.diskCacheStrategy(DiskCacheStrategy.NONE)
.into(imageView);
Solution 3:
TL;DR.
-
you can skip caching by adding the following lines:
Glide.with(context) // ... (your usual stuff) // then this .diskCacheStrategy(DiskCacheStrategy.NONE) .skipMemoryCache(true) .signature(new StringSignature(String.valueOf(System.currentTimeMillis())));
-
OR : you can workaround caching by adding a dummy random argument to your
URL
:Glide.with(context) .load(url + "?rand=" + (new Random().nextInt())) .into(imageView);
What's happening?
When your picture is loaded the first time, it's stored locally in what's called a cached memory (or simply "a cache"). When you request it for a second time Glide fetches if from the cache as if it was a successful request. This is meant for many good reasons such as: offloading your server, saving your users some data and responding quickly (offering your users a smooth experience).
What to do?
Now, concerning your issue: you need to disable the cache in order to force Glide to fetch your image remotely every time you ask for it. You can do the following:
Glide.with(context)
.load(url)
.diskCacheStrategy(DiskCacheStrategy.NONE)
.skipMemoryCache(true)
.signature(imageVersion) // supposing that each new image has its own version number
.into(imageView);
Or, in the case where you can't know when a picture is changes (no imageVersion
), use a unique signature for each picture.
Glide.with(context)
.load(url)
.signature(new StringSignature(String.valueOf(System.currentTimeMillis())))
.into(imageView);
Another clean way would be to configure your picture's cache strategy in your server and use .diskCacheStrategy(DiskCacheStrategy.SOURCE)
.
A hacky trick worth mentioning
If adding a dummy GET paramter to your target URL doesn't break anything, then you could simply workaround caching by passing a random argument each time you fetch the image, this will push Glide to think you're querying a new endpoint, thus, it won't check the cache.
Glide.with(context)
.load(url + "?rand=" + (new Random().nextInt())) // "&rand=" if your url already has some GET params
.into(imageView);
Glide caching API here.