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
Related
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
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
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();
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
Can Android Volley (Google IO 2013) have a Bitmap cache for level one of cache and also a disk cache for level2. I am not clear on if this is an or choice or an either/or choice. Also wondering about performance of Disk cache vs Bitmap cache for images. I notice that the ImageLoader seems to take either a disk cache or a bitmap cache, but I have also read somewhere about it having a level1 and level2 caching ...
Volley, by default, caches everything on disk (L2) based http headers. If there are no cache or TTL headers available, no disk caching will occure.
You asked a question regarding caching which has an answer that'll help you understand here.
About the Bitmap Cache. In fact, the ImageLoader class expects an implementation of the interface ImageCache which should be a memory cache (L1). See this question.
Out of the box Volley has only disk cache (DiskBasedCache class), but you can provide yours (implement com.android.volley.Cache interface). There is no such term as "Bitmap cache" in Volley. All the data (bitmaps, texts, etc.) is cached on disk by default.
I know it is an old question, but I had the same problem and took me couple of days reading blogs, watching videos and scrolling up and down in Volley source code to finally figure it out. So for the future reference here is the thing in the clearest way to explain:
Volley caches EVERY response unless the response explicitly says "no-cache" or "no-store" in its header. if so, it won't be cached otherwise it will be cached according to "max-age" of the response header.
The caching system above makes sense 100% since a response wont be valid if server says so that makes you capable of setting expiration date for each response from server side (It is awesome).
if you don't like the above caching method you can override the corresponding part of code in volley source code, but it is highly NOT-recommended
All mentioned above is cached on Disk using LRU algorithm which makes it L2. so volley has built in L2 caching for EVERY request (including images)
Disk cache is blocking I/O. so lets say user swipes your ListView very fast and images should load very quickly, but the I/O blocks your main thread and you see some annoying jumping while scrolling. Then a non-blocking (in-memory) L1 cache comes handy and as you can see it is just useful when dealing with images. OK then, there is no built in L1 cache in Volley but IF you want to use the ImageLoader you can code your own and bind it to the ImageLoader. (Don't bother, thousands are already available under LruBitmapCache name, just copy one)
Conclusion: Set your server to send proper caching data for your responses (which is very easy) and define an LruBitmapCache for your ImageLoader in Volley and all is done. You will have L1 and L2 caches. if L1 failes volley checks L2 (disk) and if fails again it goes for the RPC.
Hope it helps