Does anyone know of a way to remove entries from Picasso default Cache / invalidate?
I went over their api (Also Picasso Cache class ) and couldn't find any way of doing it. I'm not interested in skipMemoryCache because most of the time the image Should be loaded from the cache.
Thanks!
Roy
Now it's possible to invalidate cache using Picasso.invalidate().
Usage examples:
Picasso.with(context).invalidate(uri);
Picasso.with(context).invalidate(pathString);
Picasso.with(context).invalidate(file);
Related
I am trying to cache picture using picasso after doing some home work I figured out picasso doesn't direct cache images. Hence using help from https://gist.github.com/fada21/10655652
This is sucessfully caching image to folders. I can see the files but they don't reload when the phone is offline.
I am loading the images this way:
PicassoBigCache.INSTANCE.getPicassoBigCache(getContext().getApplicationContext()).load(pd.getImage()).placeholder(R.drawable.defaultloading).error(R.drawable.none).resize(238, 250).into(holder.image);
I fixed the loading from cache issue like this:
I used https://gist.github.com/fada21/10655652
And then did the following:
added a variable to check if internet is exists or not...
if (isnetworkavailable.equalsIgnoreCase(mContext.getResources().getString(R.string.yesnetwork)))
{
PicassoBigCache.INSTANCE.getPicassoBigCache(getContext().getApplicationContext()).load(pd.getImage()).placeholder(R.drawable.defaultloading).error(R.drawable.none).resize(238, 250).into(holder.image);
}
else
{
PicassoBigCache.INSTANCE.getPicassoBigCache(getContext().getApplicationContext()).load(pd.getImage()).placeholder(R.drawable.defaultloading).error(R.drawable.none).resize(238, 250).networkPolicy(NetworkPolicy.OFFLINE).into(holder.image);
}
This works fine if I close the app with system.exit or force close.
Not sure if this is right way but I am getting the results. Things are working smoothly. Hope this is useful for somebody!
I'm writing an app which needs to load a lot of images from the internet (a manga reader). I need to cache some thumbnail images for offline use, any others should be cleared when app closed.
I read some about cache invalidation on Glide page, they said the best way is to change the content url, but how Glide know if it is a modified url of old content or a new one? I'm new to Glide here.
https://github.com/bumptech/glide/wiki/Caching-and-Cache-Invalidation
Thank in advance :)
You can use following to load image:
Glide.with(context)
.signature(new StringSignature(yourVersionMetadata))
.into(imageView)
Just change yourVersionMetadata when you load image and it will not load from cache if yourVersionMetadata is different.
I have been trying to use Fresco library by Facebook.
From the documentation I understood that the caching of images works out of the box with the ImagePipelines when we use the SimpleDraweeView. I pretty much see things working perfectly only with the minor hiccup that eventhough Fresco fetches images from my URI the images in the DiskCache and other Caches are not replaced by it.
I'm positive about my code fetching the images because my images do show up the first time I run the app after I clear the cache and I implemented a RequestListener and a ControllerListener which both turned up success.
Here is my current setup:
// Setting the collaborator image.
GenericDraweeHierarchy collaboratorPicHierarchy =
genericDraweeHierarchyBuilder
.setPlaceholderImage(
getInitialsAsDrawable(
collaborator.getUser().getInitials()
)
)
.setRoundingParams(new RoundingParams()
.setRoundAsCircle(true)
)
.build();
holder.collaborator.setHierarchy(collaboratorPicHierarchy);
holder.collaborator.setImageURI(
Uri.parse(AltEngine.formURL("user/getProfilePic/") +
accessToken + "/" +
collaborator.getUser().getUuid()
)
);
All this code lies inside an Adapter and in the Adapter's constructor I have initialized Fresco.
imagePipelineConfig = ImagePipelineConfig.newBuilder(this.context)
.build();
Fresco.initialize(this.context, imagePipelineConfig);
After a lot of searching in StackOverflow I found that the following Snippet could be used.
Fresco.getImagePipelineFactory().getMainDiskStorageCache().remove(new SimpleCacheKey(uri.toString()));
Fresco.getImagePipelineFactory().getSmallImageDiskStorageCache().remove(new SimpleCacheKey(uri.toString()));
I tried that in the onRequestSuccess of ImageRequest instance which I associated with the SimpleDraweeView but that resulted in the placeholder being shown everytime I refresh the ListView which is bad.
The next solution I found was from here Android - How to get image file from Fresco disk cache? which suggested that it might be necessary to implement my own DiskCacheConfig for Fresco to invalidate my DiskCache so I tried configured my DiskCache with the code I found in the SO question.
DiskCacheConfig diskCacheConfig = DiskCacheConfig.newBuilder().setBaseDirectoryPath(this.context.getCacheDir())
.setBaseDirectoryName("v1")
.setMaxCacheSize(100 * ByteConstants.MB)
.setMaxCacheSizeOnLowDiskSpace(10 * ByteConstants.MB)
.setMaxCacheSizeOnVeryLowDiskSpace(5 * ByteConstants.MB)
.setVersion(1)
.build();
imagePipelineConfig = ImagePipelineConfig.newBuilder(this.context)
.setMainDiskCacheConfig(diskCacheConfig)
.build();
Fresco.initialize(this.context, imagePipelineConfig);
Still the Cache is not updated by the image from the network.
I dont know what has gone wrong here. May be I've got Fresco wrong altogether. Any how I am stuck here and dont have a clue on how to proceed. I have been through the docs a couple of times and due to my hard luck I might have missed something important. Please point me in a proper direction.
This is basically the same question as was asked here: https://github.com/facebook/fresco/issues/124.
And in fact, you had about the right idea in the two lines you pasted above with the removal from disk cache. You just need to call them in the right place. onRequestSuccess is not the right place since that will nuke the image you just downloaded.
You want to remove the old entry from cache before even sending out a request for a new one. If Fresco finds the image on disk, it won't even send out a network request.
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.
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.
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();
}
I am using universal image loader library for loading the images in my listview. I want to clear the cache for all the images at once.
I know how to do it for one image at a time.
MemoryCacheUtils.removeFromCache("file://"+path, ImageLoader.getInstance().getMemoryCache());
DiskCacheUtils.removeFromCache("file://"+path, ImageLoader.getInstance().getDiskCache());
How can I do this?
You can do that by:
imageLoader.clearMemoryCache();
or
imageLoader.getDiskCache().clear();
it depends on your configuration.
You can clear the memory cache and the disk cache distinctly. For each just call:
ImageLoader.getInstance().clearMemoryCache();
ImageLoader.getInstance().clearDiskCache();
It will erase all stored cache data.