Hi,
First of all, thank you very much for an awesome library.
I switched from Picasso to Glide, and I like Glide more than Picasso. However,
I've noticed that Glide would load the old image (deleted) instead of the new image.
For example,
I have the following images on my listview:
Image1
Image2
Image3
etc.
I deleted Image1, and renamed Image2 to Image1.
Glide would display the old picture of Image1 which was deleted instead of Image2 picture.
I went online and searched for the solution, they said that I can clear the caches with the following:
public void Clear_GlideCaches() {
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
Glide.get(MainActivity.this).clearMemory();
}}, 0);
AsyncTask.execute(new Runnable() {
@Override
public void run() {
Glide.get(MainActivity.this).clearDiskCache();
}
});
}
This solution didn't seem to work for my situation as Glide still loads the old deleted images.
Here is my listview adapter:
@Override public View getView(int position, View recycledView, ViewGroup container) {
ViewHolder viewHolder = new ViewHolder();
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (recycledView == null) {
recycledView = inflater.inflate(R.layout.listview_layout, container, false);
viewHolder.layout = (RelativeLayout)recycledView.findViewById(R.id.ListViewLayout);
viewHolder.imageView = (ImageView)recycledView.findViewById(R.id.ListViewImage);
recycledView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder)recycledView.getTag();
}
Glide
.with(mContext)
.load(getItem(position).getImageUri())
.centerCrop()
.placeholder(R.color.black)
.crossFade()
.into(viewHolder.imageView);
return recycledView;
}
class ViewHolder {
RelativeLayout layout;
ImageView imageView;
}
On Picasso,
I was able to clear the image cache for specific image without any issues.
Here is the code:
public class ClearPicassoCaches {
public static void clearCache (Picasso p) {
p.cache.clear();
}
}
In ListView Adapter:
public void RefreshPicassoCache(String path) {
Picasso.with(mContext).invalidate("file:///" + path);
}
In MainActivity:
listViewAdapter.RefreshPicassoCache(NewPath); //to refresh the specific image cache.
My questions are:
1)is there a way to refresh (reset) the cache for specific image?
2) is there anyway I can clear the Glide caches, so that it won't display the old deleted images when the deleted and the new images have the same name.?
I am testing my app on Android 5.1.1 (Lollittlepoop), GN4
Glide Version: 3.7.0
I hope someone can help me,
Thank you very much
.skipMemoryCache(true)
.diskCacheStrategy(DiskCacheStrategy.NONE)
is not a good solution as it would hurt the performance of the listview alot
You're likely calling Clear_GlideCaches() while you're looking at the list, which means the images are still loaded. Even if you clear the caches those images will be returned to the memory cache, so when you scroll away or go back and reopen the list the memory cache is hit. Make sure no images are visible when you clear caches while debugging. Best if you call that method and then immediately force stop the app, or just clear caches from App Info in Settings. While debugging issues I use a variation on that method, see: ClearCachesTask.
But this is just treating the surface problem. The deeper issue is that you shouldn't manually manage the cache, especially not as a core part of the user's workflow. A really important difference between Picasso and Glide is that Picasso was originally built on top of loading via String only, while Glide was designed with any model in mind from the beginning, so that invalidate method is impossible to implement in Glide as of now, because it can handle arbitrary objects and there's no association between objects and cache files (more is needed to hit the cache).
There's isn't really fine-grained answer to your questions other than: use .signature(...). It's a powerful concept: it mixes in anything given to it to the cache lookup, so if you use File metadata as signature you can get it to refresh in that weird use case, but keep hitting the cache normally. I would suggest mixing in file size and file date. Read https://github.com/bumptech/glide/wiki/Caching-and-Cache-Invalidation for more, and take a look at Glide's Key implementations and #699 for a third party example. I'm going to close it as duplicate of #624, feel free to continue the conversation here if you're having trouble implementing this.
Thanks for your comments.
I don't understand what you mean by "You're likely calling Clear_GlideCaches() while you're looking at the list, which means the images are still loaded."
I deleted the image and this image was deleted of my listview.
I have been to this link before:
https://github.com/bumptech/glide/wiki/Caching-and-Cache-Invalidation
but I have no clue what you mean by "yourVersionMetadata" or "dateModified"
Thanks a lot
I deleted the image and this image was deleted of my listview.
This doesn't mean that the image is unloaded. You have to implement RV.Adapter.onViewRecycled and call Glide.clear to unbind the image. Otherwise it may stay dangling in memory until the mContext is destroyed, see http://stackoverflow.com/a/32887693/253468. This unbinding is needed if you support removing items from the adapter. In the most basic lists we mostly have static data and list items are just simply recycled in which case the new load clears the old one before continuing.
I have no clue what you mean by "yourVersionMetadata" or "dateModified"
If you follow the logic of yourFragment and yourImageView which I'm sure is clear what they mean, it should be that yourVersionMetadata is some metadata that you define depending on how you're versioning files. In my suggestion above the easiest would be new StringSignature(file.length() + "@" + file.lastModified()) where File file = ....
The media store signature is there if you're querying the content provider in Android instead of walking the file system yourself. The cursor it returns should contain these columns as well, so when you start a load you should have those ready.
I tested this:
.signature(new StringSignature(file.length() + "@" + file.lastModified())
It worked and I don't need to use Clear_GlideCaches() method anymore.
Thank you very much for your help
Hi,
There's no class called StringSignature in the glide package. Do I need some other dependency for that?
@rohan20 It's been generalized as ObjectKey in v4.
Worked. Thanks! 馃憤
I have the same issue but till now it is not solved. can you write the sample code for glide please.
.skipMemoryCache(true) .diskCacheStrategy(DiskCacheStrategy.NONE)is not a good solution as it would hurt the performance of the listview alot
Worked. nice solution
Most helpful comment
is not a good solution as it would hurt the performance of the listview alot