I'm using Picasso in my application for loading image efficiently and is doing his job at the best.The issue is that, currently if i call Picasso.with()
multiple times with same url, however each time with an image view having different dimensions, the image gets downloaded again.In Glide
we have the methods .diskCacheStrategy()
with DiskCacheStrategy.SOURCE
for resolving the problem.is there any alternative way in Picasso ?
In this query we will get the solution for Glid but not for Picasso.how can i reuse the image without redownloading for different dimensions ImageView.
This is the code I'm using
Picasso.with(context)
.load(URI)
.placeholder(R.drawable.ic_profile_thumb)
.resize(180, 180).centerInside()
.into(viewHolder.imgThumbnail);
resize(screenWidth, imageHeight) . centerInside() . into(imageView);
This example demonstrates how to do I in android. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 3 − Add the following code to res/layout/activity_main.
Image loading using Picasso is very easy, you can do it like this way Picasso. get(). load("http://i.imgur.com/DvpvklR.png").into(imageView); and in their website you can get every details. In your case you can parse every image URL and use RecyclerView to show them along with Picasso.
Picasso does not cache the image to disk, Picasso only has a memory cache. the Disk cache is done by the networking library(OKhttp). so the image written to the disk cache is never re-sized.
if the same url image is downloaded multiple time you might have to check your networking library.
If your project is using the okhttp library then picasso will automatically use it as the default downloader. If you are are using any other network library then you will have to write your own downloader and set it using
Picasso.Builder(context).downloader(customDownloader)
this link might make things clear
Picasso by default doesn't resize images. If you do a simple call like the next one...
Picasso.with(context).load("https://goo.gl/v9EkbF").into(imageView);
...Picasso is caching the full-size image. From that moment, every time you call the same URL, Picasso will reuse that full-size image and let the GPU do the resizing into the view.
Use the resize() method you are only caching the resized image, and Picasso will need to download the image again if you use a different size.
Don't use the resize() option and you'll get better bandwidth usage, but be careful with memory usage.
The defaults in Picasso
Picasso does have automatic memory and disk caching. Any image that was requested recently will be in memory cache. If it isn't there Picasso will check the disk cache. If it's not available on disk, only then Picasso will start the network request.
All requested images are stored in both caches until they have to be deleted in order to free space.
The default's in 2.5.2 are:
You can increase the disk cache like this (example for 100MB):
Picasso picasso = new Picasso
.Builder(this)
.downloader(new OkHttpDownloader(getCacheDir(), 100000000))
.build();
Picasso.setSingletonInstance(picasso);
Changing the disc cache size does not change the cache policy.
Memory Policies
Example
Picasso
.with(context)
.load(https://goo.gl/v9EkbF)
.memoryPolicy(MemoryPolicy.NO_CACHE, MemoryPolicy.NO_STORE)
.into(imageView);
Network Policies
Example:
Picasso
.with(context)
.load(https://goo.gl/v9EkbF)
.networkPolicy(NetworkPolicy.NO_CACHE)
.into(imageView);
Memory and caching considerations
RGB565 is half the size of RGB888. The result of using RGB565 and smaller images is that Picasso memory footprint is higher (might be 3x/4x) than Glide's.
Those are just the defaults. Glide can be configured to use RGB888 and Picasso to use RGB565. Picasso can be configured to throw into memory just the resized images like Glide.
Under the same setups, the memory footprint is almost the same.
Bandwidth considerations
You can ask Glide to cache everything, full-size image and resized images but by default Glide's bandwidth consumption is higher.
Note
Picasso 2.6 is coming and I have no idea about what's different/new.
Sources
You can use OkHttp
from the same vendor "SquareUp".
This answer show you how to do it with details.
UPDATE: The links were old, by the way, Here are the changes in the newest versions:
https://github.com/square/picasso/blob/master/CHANGELOG.md
And as @Sotti said, Picasso
has Automatic memory and disk caching
which they mentioned about that in the Introduction section:(the last part)**
http://square.github.io/picasso/#introduction
In this query we will get the solution for Glid but not for Picasso.how can i reuse the image without redownloading for different dimensions
ImageView
.
Actually, Picasso
only has a memory cache, But you may want to use OkHttp
for that.
Check this link: https://stackoverflow.com/a/18552559/4409113 Which says:
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.
Check this out too: https://corner.squareup.com/2013/05/picasso-one-dot-oh.html
Picasso automatically utilizes a memory and disk cache (provided by the
HTTP client
) to speed up bitmap loading. For development you can enable the display of a colored marker which indicates the image source.
And also, check this question: How to implement my own disk cache with picasso library - Android?
To save the files in custom cache directory using OkHttp
:
OkHttpClient okHttpClient = new OkHttpClient();
File customCacheDirectory = new File(Environment.getExternalStorageDirectory().getAbsoluteFile() + "/MyCache");
okHttpClient.setCache(new Cache(customCacheDirectory, Integer.MAX_VALUE));
OkHttpDownloader okHttpDownloader = new OkHttpDownloader(okHttpClient);
Picasso picasso = new Picasso.Builder(mainActivity).downloader(okHttpDownloader).build();
picasso.load(imageURL).into(viewHolder.image);
From: https://stackoverflow.com/a/33276054/4409113
As you can see, uses setCache
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With