Whether picasso supports image disk caching - android

Before I had worked on Volley and I have used the DiskLruCache [link] with Volley to cache images on disk.
Now I have been working with app that is using Picasso.
I would like to know whether Picasso supports disk caching.
If it supports how can I fix the cache size.
Which will be useful when loading images from remote with disk caching?

Picasso supports disk caching, and it's relying on the HTTP client for this.
If you're using it with OkHttp, the default size for the disk cache will be around 50 MB (2% of total space, max 50MB, min 5MB).
If this doesn't meet your needs, you can either implement your own disk cache or manually initialise the OkHttpDownloader with a larger disk cache size when you initialise your Picasso using the Picasso.Builder.
I would recommend the latter, it should look something like
new Picasso.Builder(context).downloader(new OkHttpDownloader(MAX_CACHE_SIZE)).build();

Related

Is it safe to use Glide Caching instead of Internal Storage

I have a scenario where i have to keep few images in offline mode. Now i am confused here that should i rely on Glide's cache or I have to put those images in Internal Storage?
Hopefully Glide will work, but need to know if there exists any limitation if i go with Glide. Here word cache is causing confusion.
Here is the information about underlying memory usage by Glide.
Glide uses DiskLruCacheWrapper as the default DiskCache. DiskLruCacheWrapper is a fixed size disk cache with LRU eviction. The default disk cache size is 250 MB and is placed in a specific directory in the Application’s cache folder.
Further underlying complexities of Glide are here. Hope confusion about the max memory Glide uses is cleared here and if customization is needed, that can also be achieved by following the link mentioned.

Does Picasso cache only original downloaded image's size?

I read something about this issues but I didnt understand as well:
1: if I .resize(x,y) an image in my code, then Picasso caches only the original size or the resized one too?
2: memory and disk cache are storing with different cache-key?
Yes and no.
Picasso caches resized images. If you call resize(50, 50) on a URL twice the second request will use the resized image which was cached in memory. The HTTP client will never see the URL a second time in this case.
The HTTP client that Picasso uses will cache the original image (if configured to do so and the headers allow it). If you call resize(50, 50) on a URL the original will be cached on disk by the HTTP client so that if you call resize(100, 100) on that URL the HTTP client does not need to fetch the image again.
The HTTP client cache is completely opaque to Picasso. It doesn't see or control its contents. Picasso makes an HTTP request for every URL that isn't available in the memory cache and whether the HTTP client downloads it or serves it from the local cache is completely controlled inside the HTTP client.
1 - Yes, picasso only caches the original size.
2 - Yes.
The global default Picasso instance returned from with() is automatically initialized with defaults that are suitable to most implementations.
LRU memory cache of 15% the available application RAM
Disk cache of 2% storage space up to 50MB but no less than 5MB.
More information about caching in Picasso.
How do I use disk caching in Picasso?
Comparission with glide (also have infomation about caching)
http://inthecheesefactory.com/blog/get-to-know-glide-recommended-by-google/en

android picasso Image dowloading

I have activity in which there is a dialog window with some images. I want to download these images into cache when my activity starts and load them from cache when dialog window appears.
My activity code:
for(int i=0; i<avataritemlist.size();i++){
Picasso.with(activity_context)
.load(item.getpath())
.noFade();
}
Dialog adapter code:
Picasso.with(mContext)
.load(item.getpath())
.noFade()
.into(holder.imageView);
I expect to cache the image in my activty and then in dialog adapter load in from cache, but in my case it downloads it again in adapter. I want to emphasize that activity_context and mContext is the same. What am I doing wrong?
Default Picasso instance returned by Picasso.with uses an automatic memory and disk caching. It is initialized with default values:
• LRU memory cache of 15% the available application RAM
• Disk cache of 2% storage space up to 50MB but no less than 5MB. (Note: this is only available on API 14+ or if you are using a standalone library that provides a disk cache on all API levels like OkHttp)
• Three download threads for disk and network access.
How default Picasso instance does LRU memory cache is something you might not be able to know, all you know is it is a memory cache which uses a least-recently used eviction policy.
However you can create a Picasso instance using Picasso.Builder that gives you a better control over image caching (over disk). Check more on this stackoverflow post on how to set up http request header property Cache-Control with max-age and Jake Wharton's answer too
Also you may also want to try out Glide which syntactically is very similar to Picasso

How Picasso downloading images

I have a question about mechanisms used by Picasso for downloading and caching images.
How does Picasso download an image? I know that it is using in sample size. Am I right? If image on a server is 1000x1000 but ImageView is only 400x400 then it will be download only 500x500 image and it will be cached. Or maybe it will be downloaded in full resolution and then scaled to a specific size.
Here is actual code I am - and I'm sure many more people are - using
Picasso.with(context).load(url).fit().centerCrop().into(imageView);
Picasso has no way of knowing it should download only 500*500 pixels. The fit() and centercrop() methods will make it fit even when picture is bigger than needed.
You can browse source-code of Picasso at: https://github.com/square/picasso.
Downloading images
You can see that Picasso downloads images with implementation of Downloader interface. It uses default downloader named OkHttpDownloader, which utilizes OkHttp library. When it can't be loaded, Picasso uses UrlConnectionDownloader.
Recognizing size of the images
Picasso doesn't know size of the images before download. If you are developing back-end server, you can specify size of the images in some way, so your mobile application will know it by performing a concrete request, but it can't be recognized by Picasso itself. Picasso has to download image in a full size and then this image can be cropped or resized by this library.
Cache
We can find the following information about Cache in the Picasso documentation placed in source code:
Picasso instance is automatically initialized with defaults that are
suitable to most implementations.
LRU memory cache of 15% the available application RAM
Disk cache of 2% storage space up to 50MB but no less than 5MB. (Note: this is only available on API 14+ or if you are using
a standalone library that provides a disk cache on all API levels like
OkHttp)
Three download threads for disk and network access.
It explains usage of cache in this library quite clearly.
I'm just not sure, if Picasso stores images before transformation (resizing, cropping, etc.) or after transformation in cache. First option seems more reasonable for me, because we decide to apply a different transformation later, so we may want to keep original image.

How to retrieve images from cache memory in picasso?

I am using picasso library for loading images .In default picasso, It uses internal cache memory for loading images.But as per my app configuration ,i have to use external cache memory(Cache on Disk).
so i used this code for Cache on Disk
File httpCacheDir = new File(getApplicationContext().getExternalCacheDir(),"http");
long httpCacheSize = 10 * 1024 * 1024; // 10 MiB
HttpResponseCache.install(httpCacheDir, httpCacheSize);}
Picasso is flexible. So now it caches images in external Sd card..
The caches is stored in sdcard/android/data/packagename/cache/http
The caches are stored in ".1" ,".0". formats
so i just opened them and changes into ".1" to ".jpg".it gives exact images what i need.
But how to do in programatically? but picasso itself caches my memory in to my app for loading image into imageview.but i have to save them into sdcard directly as images/set bitmap as wallpaper in offline mode?
You can supply your own Cache implementation when building your Picasso instance. This way you can provide extra methods that you can call to retrieve bitmaps directly from your memory cache. Use Picasso.Builder to provide your own implementation for it. When you use with() you are using a static singleton internal instance thats setup with most of the default values (most apps need the default values anyway.)
Keep a reference of your Cache implementation around and directly interact with it. Picasso is meant to handle the loading/decoding and caching for you but there is no reason you cant build around it.
If you are referring about the disk cache, then no Picasso does not support that at the moment. This is by design because the disk layer cache is done by the HTTP layer and makes no distinction about it.
You could however, change the path of the disk cache. If you are using OkHttpDownloader then supply a different file when you construct your Downloader. Similarly for UrlConnectionDownloader you could extend it and override the load() method.
Picasso does handle the caching in it and downloading also you just need to place it in your target Image view similar to Aquery
According to The Corner Square Engineering blog
picasso handle downloading caching in it self and give its handler to user to use it and place the image in image view

Categories

Resources