How to reload image in Glide from the same url?
Asked Answered
H

6

18

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?

Hymie answered 19/12, 2017 at 11:42 Comment(0)
S
28

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

Sportscast answered 19/12, 2017 at 11:52 Comment(5)
what if I want to load the image only when it's URL is changed?Onwards
Latest docs for Glide 4 signatures: bumptech.github.io/glide/doc/caching.html#cache-invalidationTwentyfourmo
Neither of these show up as valid methods when I try itGunplay
OMG. I cannot believe this is best answer, and this is repeated in other answers. .signature(ObjectKey(System.currentTimeMillis().toString())) is not datetime of last update (nor we could magically know file was last updated on the server without hitting it), it is just signature based on current time, so changes with every milisecond. This is no caching, every time this call is called again to load same image into other view (and more then milisecond has passed) image will be fetched from server again.Waterer
@Waterer is correct. Use the file's last modified date rather than current time. Example: .signature(new ObjectKey(imageFile.lastModified()))Hirst
A
6

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);
Allanson answered 19/12, 2017 at 11:44 Comment(1)
what if I want to load the image only when it's URL is changed?Onwards
I
5

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)
Iolenta answered 11/2, 2019 at 8:42 Comment(1)
This is just invalidation of cache if more then milisecond has passed since file was cached. Better have no caching at all than this.Waterer
V
1

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);
Vortical answered 19/12, 2017 at 11:47 Comment(0)
H
0

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

Hymie answered 19/12, 2017 at 16:37 Comment(1)
This is just invalidation of cache if more then milisecond has passed since file was cached. Better have no caching at all than this.Waterer
M
0

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.

Mystic answered 29/1, 2021 at 19:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.