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?
Related
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.
I would like to know if Glide handles image loading asynchronously or do I have to use AsyncTask myself to handle the impact on the main thread?
I just need better clarification on this so that whenever I decide to load images into for arguments sake, a RecyclerView I can know for sure that Glide is just as good to use as Picasso (Where asynchronous media handling is concerned).
I do know that Picasso loads images asynchronously but does Glide?
I'm more familiar with volley but I took at look at Glide for you. It seems like there's a cache, so the answer should be yes, Glide should load your image asynchronously. You do not have to handle the impact on your main thread.
The cache will load and store your images for you. For valley, the code is as simple as:
ImageLoader imageLoader = AppController.getInstance().getImageLoader();
// thumbnail image
NetworkImageView thumbNail = (NetworkImageView) view.findViewById(R.id.thumbnail);
thumbNail.setImageUrl(posterURL, imageLoader);
ImageLoader object probably has the cache which loads and holds the images.
Yes it does. Glide's API is the same as Picasso's one, and their implementations are also nearly the same (their difference is mainly in their cache implementation)
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);
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.
I'm dynamically generate NetworkImageView for an Internet image, and it starts to fetch real file when the view is created.
Is there a way to force Volley library to cache files of some url, before I really create the view? Or do I need to use ImageRequest and handle the cached files by myself?
Thank you!
I think you use ImageLoader where you can put a string url and I want to believe it caches it, but there is also a bitmap listener (to let you know when it is done loading), so I'm not sure if this can just be added to the network call to your server