Glide not updating image android of same url?

主宰稳场 提交于 2019-12-03 22:18:24
//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);

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

This is an old question, so I'm sure you've got your fix by now. However I believe both of the big libraries(Picasso and Glide) now support something similar.

Glide has its signature API you may wish to look into: https://github.com/bumptech/glide/wiki/Caching-and-Cache-Invalidation

Glide.with(yourFragment)
    .load(yourFileDataModel)
    .signature(new StringSignature(yourVersionMetadata))
    .into(yourImageView);

I've not had a chance to work with this myself, but it looks as though you need to make a call to your API to get the version Metadata, then a subsequent call to fulfil it.

Alternatively, look into ETags.

Good luck!

I my case Glide not recognize .signature(), so additionally to @MGDavies's answer you maybe need something like this

// in kotlin
Glide.with(context)
        .load("url")
        //using apply to RequestOptions instead signature directly
        .apply(RequestOptions().signature(ObjectKey("time that image was updated")))
        .into(thumb)

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 offering them a smooth user experience by responding quickly.

Now, concerning your issue. You need to disable the cache to force Glide to fetch your image remotely every time you ask for it. Just do the following:

Glide.with(context)
         .load(Constants.COPY_LINK_BASE_URL + info.getDisplayPicture())
         .placeholder(R.drawable.ic_profile).dontAnimate()
         .diskCacheStrategy(DiskCacheStrategy.NONE)
         .skipMemoryCache(true)
         .signature(new (SettingManager.getUserPictureVersion(context)))
         .into(ivUserProfilePhoto);

Or use a unique signature for each picture. (Kinda hacky but does the trick)

Glide.with(context)
         .load(Constants.COPY_LINK_BASE_URL + info.getDisplayPicture())
         .placeholder(R.drawable.ic_profile).dontAnimate()
         .signature(new StringSignature(String.valueOf(System.currentTimeMillis())))
         .into(ivUserProfilePhoto);

Another clean way would be to configure your picture's cache strategy in your server and use .diskCacheStrategy(DiskCacheStrategy.SOURCE).

Further reading

Glide caching API here.

When new images are loaded or updated, use below to clear Glide memory and cache :

|==| Clear Glide memory

 Glide.get(getApplicationContext()).clearMemory();

|==| Clear Glide Cache()

void clearGlideDiskCache()
{
    new Thread(new Runnable()
    {
        @Override
        public void run()
        {
            Glide.get(getApplicationContext()).clearDiskCache();
        }
    }).start();
}

Now Glide latest version, you have to use RequestOption for clear cache.

RequestOptions options = new RequestOptions()
.diskCacheStrategy(DiskCacheStrategy.NONE)
.skipMemoryCache(true);

Or for everytime load picture you have to use a String in as signature.

RequestOptions options = new RequestOptions()
.signature(new StringSignature(String.valueOf(System.currentTimeMillis())));

Finally:

Glide.with(CompressSingleActivity.this)
.applyDefaultRequestOptions(options)
.load(currentFile)
.into(currentImageView);

it's like the issue is related to Glide Library itself, I found the trick that can fix this, just put a random number after your image URL as a query like an Example code below, it fixed my problem I hope it helps you too

        Random random = new Random();
        int randomInt = random.nextInt();
        Glide.with(context)
                .applyDefaultRequestOptions(new RequestOptions()
                        .circleCrop().diskCacheStrategy(DiskCacheStrategy.NONE))
                .load(ConstantVars.BaseUrl + userModel.getAvatarURL() + "?" + randomInt)
                .apply(new RequestOptions().error(R.drawable.himart_place_holder))
                .into(imageViewProfile);

it will make Glide to reload image anytime you request like there's no cache at all to read from.

Glide 4.8.0

Glide.with(mContext).asBitmap()
                    .load(nopicUrl)
                    .apply(RequestOptions.skipMemoryCacheOf(true))
                    .apply(RequestOptions.signatureOf(new ObjectKey(String.valueOf(System.currentTimeMillis()))))
                    .apply(RequestOptions.diskCacheStrategyOf(DiskCacheStrategy.DATA))
                    .into(ivImageView);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!