Android Image invalidate in fresco

2019-05-31 09:06发布

问题:

I am migrating android image caching library from picasso to fresco. I want to know if there is any way to invalidate image already catched as I am adding feature to replace existing image there is way to do so in picasso like

Picasso.with(context).invalidate(URI);

This line remove the cached image and use new one using the url provided which is same like,

http://example.com/image_path

In fresco I have tried using

Fresco.getImagePipeline().evictFromMemoryCache(uri);

This is removing image from view but adding same old cached image again and not getting new one from network as it is working in picasso.


Please refer question Invalidate cache in Picasso The accepted answer doing great in case of picasso.

回答1:

Fresco.getImagePipeline().evictFromMemoryCache(uri);

Above code line remove the image from the catche but image remains there in the disk and render same if called. We need to remove same image from disk as well. Bellow two lines remove the the image from disc cache also we need to remove the small that is thumbnail image if saved from disk cache.

Fresco.getImagePipelineFactory().getMainDiskStorageCache().remove(new SimpleCacheKey(uri.toString()));
Fresco.getImagePipelineFactory().getSmallImageDiskStorageCache().remove(new SimpleCacheKey(uri.toString()));

Note: if you are using custom cache key you need to change it that way.



回答2:

Try this

public static void clearCache(){
            //
            ImagePipeline imagePipeline = com.facebook.drawee.backends.pipeline.Fresco.getImagePipeline();
            imagePipeline.clearMemoryCaches();
            imagePipeline.clearDiskCaches();
            // combines above two lines
            imagePipeline.clearCaches();
        }