2

I know it is a very basic question. I have tried to find the numerous solution but I am not able to understand them.

What I want

upload image to the server and in return I am getting URL but the problem is while setting the image using this URL, the old image is set. This is happening because the glide is taking old cache and not updating the cache.

How to solve this.

Glide.clear(profilePic);

Glide.with(getApplicationContext())
    .load(url)
    .diskCacheStrategy(DiskCacheStrategy.ALL)
    .skipMemoryCache(true)
    .transform(new CircleTransform(MainProfile.this))
    .into(profilePic);

currently, the pic is changed but when I click the back button and come back to this activity then it loads an old image. Loading the image from cache like that.

//setting up the profile pic
Glide.with(getApplicationContext())
.load(userProfilePicUrl)
.asBitmap()
.centerCrop()
.into(new BitmapImageViewTarget(profilePic) {
    @Override
    protected void setResource(Bitmap resource) {
        RoundedBitmapDrawable circularBitmapDrawable =
                                RoundedBitmapDrawableFactory.create(MainProfile.this.getResources(), resource);
        circularBitmapDrawable.setCircular(true);

        profilePic.setImageDrawable(circularBitmapDrawable);
    }
});

The problem is when I come back to this activity it shows old pic instead of new one.

Zoe
  • 27,060
  • 21
  • 118
  • 148
Ankur Khandelwal
  • 1,042
  • 3
  • 19
  • 40

4 Answers4

5

RequestOptions provides type independent options to customize loads with Glide in the latest versions of Glide.

Make a RequestOptions Object and use it when we are loading the image.

RequestOptions requestOptions = new RequestOptions()
    .diskCacheStrategy(DiskCacheStrategy.NONE) // because file name is always same
    .skipMemoryCache(true);

Glide.with(this)
    .load(photoUrl)
    .apply(requestOptions)
    .into(profile_image);
Jason Roman
  • 8,146
  • 10
  • 35
  • 40
Vinay John
  • 990
  • 12
  • 13
3

Try this out

Glide.with(DemoActivity.this)
.load(Uri.parse("file://" + imagePath))
.diskCacheStrategy(DiskCacheStrategy.NONE)
.skipMemoryCache(true)
.into(mImage);

Replacing DiskCacheStrategy.ALL to DiskCacheStrategy.NONE

android_griezmann
  • 3,757
  • 4
  • 16
  • 43
2

Maybe you can try this:

Glide.get(context).clearDiskCache()

Try reading this link as a ref

However this solution also seems more provided.

Glide.with(Activity.this)
.load(Uri)
.diskCacheStrategy(DiskCacheStrategy.NONE)
.skipMemoryCache(true)
.into(Image);

Reference to which is similar question asked before, which you can find here.

Hope that helps somewhat.

Cheers

Community
  • 1
  • 1
Kwekuq
  • 123
  • 1
  • 1
  • 10
0

Glide has inbuilt function to invalidate cache.By using signature() function old cache can be invalidated.

GlideApp.with(MainProfile.this)
.load(mediaStoreUri)
.signature(new MediaStoreSignature(mimeType, dateModified, orientation))
.into(view);
Vikas Chauhan
  • 51
  • 1
  • 2