Picasso - force fetch/load - android

I need to cache all images from response (currectly I am using OkHttpClient, OkHttpDownloader and Picasso classes). And caching looks like it is working.
But all images in the response have timestamp where is last update time of that image. It is possible to force picasso to re-fetch/re-load image to cache if the timestamp will be outdated? But I dont want to invalidate whole cache - just that one image.

Related

How to clear glide cache only when changing firebase-storage picture?

I want to know how to clear the glide cache with something similar to this:
.diskCacheStrategy(DiskCacheStrategy.NONE)
.skipMemoryCache(true)
But only when the user changes his profilepicture (the target) that he has saved under images/$userid
Right now, the picture just gets taken from the cache and isn't up to date with the picture on the server, but for performance reasons I cant reload the picture everytime.
What would the best practise be in my case?

get bitmap after picasso sets image to an ImageView

I'm trying to save downloaded images in shared preferences in base64 format so that the app can work offline. I've used picasso
Picasso.with(mContext).load(urls.get(position)).into(imageView);
I request the server with a specific category of images and the server responds with a json containing the urls of those images. I found picasso to do this job for me as i need to write my async task for this.The images are loaded into ImageViews of a gridview image adapter.
What I want to do is that as soon as the image is loaded through the url using this function I want that bitmap image so that I can save it in shared preferences or serialize it something so that it can be used offline.
or suggest me a better solution.
Solved it!
Picasso.with(mContext)
.load(url)
.networkPolicy(NetworkPolicy.OFFLINE)
.into(imageView);
Thankyou so much guys <3

Picasso: Call fetch on same URL multiple times

I want to pre-fetch images using the Picasso library. In the documentation of Picasso I find the description for fetch
public void fetch():
Asynchronously fulfills the request without a ImageView or Target. This is useful when you want to warm up the cache with an image.
Note: It is safe to invoke this method from any thread.
For into(...) the behaviour is obviously that Picasso takes the image from the cache (if still available).
But what happens when I call fetch multiple times for the same image / url? Is the image re-fetched (from the network) or does Picasso find the image in the cache?

Picasso - keeping images on disk

I'm using Picasso to download images in my app. My understanding is that it uses a http client (HttpResponseCache or OkHttpClient) to cache these images on disk.
Without knowing much about either of these libraries, is it possible to mark certain images as permanent? In other words, I would like to download an image and guarantee that it will be available offline.
Thinking about it, I couldn't really have the disk cache go over a certain size, so I guess what I really need is to remove the TTL on the image url and allow the cache to remove images in a first in first out scenario.
In that case, can I control which image will be deleted first (by using a timestamp based on accessed, rather than downloaded)?
Update
Based on the answer from this SO question:
Android + Picasso: changing URL cache expiration
So this answers the first part of the question - we can control the TTL through the server.
After talking to a colleague, he pointed out that the http client should take into account how frequently an image is accessed (in addition to the TTL). So hopefully I don't have to worry about this either.
That leaves me with one question. I know which images I don't need anymore, can I remove an image from the disk cache?
You can iterate the elements in OkHttp's disk cache, and call Iterator.remove() to get rid of the ones you don't want.
http://square.github.io/okhttp/javadoc/com/squareup/okhttp/Cache.html#urls--
You'd have to extend Picasso's default cache and create a custom Picasso instance to use it:
Extend LruCache
Override the void set(String key, Bitmap bitmap) method to do what you describe (adding timestamp, etc.). Check out the original source code here.
Make sure that the original trimToSize method is never called by set (and clearKeyUri for that matter), and write your own to check for the timestamp etc to get the behavior you describe
Create a custom Picasso instance with your custom cache like this:
Picasso picasso = new Picasso.Builder(context).memoryCache(cache).build(); Picasso.setSingletonInstance(picasso);
Where cache is an instance of your custom LruCache class
when you want to store the images on the disk you should use the okhttpdownloader
OkHttpClient client = new OkHttpClient.Builder()
.cache(new Cache(getCacheDir(), Integer.MAX_VALUE))
.build();
Picasso build = new Picasso.Builder(this)
.downloader(new OkHttp3Downloader(client))
.build();
Picasso.setSingletonInstance(build);

Prevent Picasso to fetch already loaded images and unable to use OkHttp2.0 for disk caching

Both of above is related. Following is my case
I am using Volley Library
Picasso(2.3.2) to load images
Images are fairly large,so I resize them to dimension 300x300
But during scroll of ListView/GridView, the images are reloaded again. Though the reload time is fairly small, I do not want the reloading of such nature.
So browsing, net I came across following
Use OkHttpClient caching mechanism
How to implement my own disk cache with picasso library - Android?
So I tried using OkHttp 2.0.0 into Volley Library
https://gist.github.com/JakeWharton/5616899
I think , from OkHttp 2.0.0 there is something to be changed on above gist.
So I followed this instead
How to implement Android Volley with OkHttp 2.0?
But the Volley library won't function now using method 2.
Finally, I am trying to use caching as mentioned on this
https://gist.github.com/ceram1/8254f7a68d81172c1669
So, my question is fairly simple, how not to reload the images that has been already downloaded. And if ,I have to use OkHttp 2.0.0 for disk caching, what are the ways, I should follow.
Stay with flow of how cache should work. Don't know Picasso cause I use Aquery, but they should use pretty much same flow whenever a bitmap(bm) needs to 'load' in some image view.
Flow:
If bm is thumb do chekthm
If bm large do cheklarge
On thmb:
If in memcache return bm for load to view
If in filecache return bm from there
In temp bm still null do network fetch to return bm
, with loads to respective caches and with 'ejects' 4 onFullCache.
For large bm , use same flow like thmb only config cache holds not as many entries.
Nowhere in there do you have to prevent a fetch . why because the process Only arrives at choice of network fetch when the bm Not avail from local options. At that point u either use default drawible from local res ( not correct bm for your logic) or you must do network fetch.

Categories

Resources