Picasso: Cache and redirected URLs - android

Here's what I currently understand:
I load http://www.example.com/imageA.png into my imageView using Picasso.
Picasso downloads imageA.png and in simple terms, caches it internally as "http://www.example.com/imageA.png".
I go offline, open my app, and Picasso tries to load http://www.example.com/imageA.png into my imageView again. It loads the image from the cache. I can see imageA even when my network isn't working.
This is all fine and dandy.
Say for example http://www.example.com/imageA.png now redirects to http://www.example.com/imageB.png and I repeat steps 1 and 2.
This time, I believe, Picasso caches it as http://www.example.com/imageB.png and therefore when I open my app in offline mode, I don't see imageA because it's cached as imageB.
Am I right?
Is there a way to force Picasso to cache it as its original URL?

Apparently you can add a stableKey to the request with the Request.Builder()
https://square.github.io/picasso/javadoc/com/squareup/picasso/Request.Builder.html#stableKey-java.lang.String-

Picasso manages memory cache only, so stableKey is only about that. Disk cache is managed by OkHttp. It's behaviour cannot be changed because com.squareup.okhttp.Cache is a final class.

Related

Glide get image cache list

here the scenario
1: Open android app with glide, then glide download the image and save it in cache.
2.if app is close then reopen, then the URL is same, glide load the image from cache
3:While not connecting to internet,when open the app, i want to make glide display random image from cache, it will be nice if glide can list all the URL from cache
how to make step 3 work?
You can use DiskCacheStrategy.
Set of available caching strategies for media.
static DiskCacheStrategy ALL
Glide uses memory and disk caching by default to avoid unnecessary
network requests.
Glide.with(contextOBJ)
.load("IMAGE_LINK")
.diskCacheStrategy(DiskCacheStrategy.ALL)
.into(imageViewOBJ);

How to clean up memory cache for Picasso?

I use Picasso for picture viewer, which is able rotate images.
Problem is that when I rotate image and try to update it in viewer, it is not updating until I "destroy" application or use memory policy - .memoryPolicy(MemoryPolicy.NO_CACHE). I tried to use app without cache, but it is quite slower, so it is not solution.
Therefore I am looking for the way to clean up image from memory and update view. I can´t use parametr .rotate(90f), because Picasso handle correct rotation by itself, it is not working only temporary until memory is clean up.
This problem is not problem of file cache, it is problem of memory cache (based on my testing with memory cache settings). I also tried to invalidate file cache to be sure, but it didn´t helped as I expected.
Picasso.with(context).invalidate(filePath);
Picasso
.with(context)
.load(filePath)
.into(imageView);
Uff, I finally found it. Before I used Glide, which has parametr signature. Picasso has something similar called stableKey. When I use this, I do not have to care about cache, I just enable it and Picasso handle rest.
Picasso
.with(context)
.load(filePath)
.stableKey(uniqueFileAttribute)
.into(imageView);

picasso doesn't cache for the first load but it's ok for the second time

I implemented a routine which saves data from a json into database and after that if user gets offline, he can see all data. but picasso doesn't load the images after the first run. but when i run the application twice in online mode, after that picasso can load the images from cache in offline mode.
(it should cache images on the first run but it's not working)
appreciate any suggestion
https://stackoverflow.com/a/23281195/3664628
Picasso doesn't have a disk cache. It delegates to whatever HTTP client you are using for that functionality (relying on HTTP cache semantics for cache control). Because of this, the behavior you seek comes for free...
The main reason may be other images are evicting the older ones from the cache due to their size. You can load smaller versions or increase the size of the memory cache like this
Picasso p = new Picasso.Builder(context)
.memoryCache(new LruCache(Size))
.build();
If you don't want to save in cache, You can Additionally exclude that too using Memory Policy.
Picasso attempts to get the requested image from the memory first. If you would like Picasso to skip this step, you can call memoryPolicy(MemoryPolicy policy, MemoryPolicy... additional) on your Picasso request creator. MemoryPolicy is a simple enum with two values: NO_CACHE and NO_STORE. like this
Picasso
.with(context)
.load(UsageExampleListViewAdapter.eatFoodyImages[1])
.memoryPolicy(MemoryPolicy.NO_CACHE, MemoryPolicy.NO_STORE)
.into(imageViewFromDisk);
Additional Source : futurestud.io

Clearing Picasso images cache

I am using the Picasso library on my Android app to load images. I would like to add an option called "Clear images cache" on my app that would remove all the downloaded images from the cache, but obviously that would remove the downloaded images from my app only (I mean not from the other apps).
Is there a simple way to do that using Picasso? Using a native component?
Thanks!
You can clear in-memory cache in Picasso only per image:
Picasso.with(context).invalidate(imagePath);
Removing all cache is somewhat tricky and described here.
File cache is delegated to HTTP Client, so it's not possible to clear it from Picasso. For more information refer this answer.
Try this line of code given below, this removes the resource inside ImageView.
Picasso.with(context).setImageResource(0);

With felipecsl Android-ImageManager library how can I replace a cached image

I am using felipecsl Android-ImageManager library to download some images from one of our servers and load it in a list view. In my Android application I have a feature to change that downloaded image from app side and upload it to server.
I do know how to load the image from URL because it is well documented with the above mentioned link. What I need is to change/delete a particular cached item and replace it with my new image from Android application side.
Let me explain it further. Normally what happens is, it loads an image from the provided URL and caches it in the manager's LRU and Disk cache. Then the second time call to that same URL, if the image is already cached, fetches it from the cache even I have set a different image from my application side.
Therefore how can I either delete that URL from cache OR set the new image for the cache of that same URL.
I have tried something like below, but seems it is not working.
imageManager.getCacheManager().getMemoryCache().put(ImageManager.getCacheKeyForJob(myUrl, jobOptions), bitmap);
That is not really supported by the library. If you need to do this kind of more advanced cache management, you can use Picasso (https://github.com/square/picasso), where you can set your own cache manager and have more control over your cached images.

Categories

Resources