Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Picasso Image downloaded again for an ImageView with different dimensions?

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);
like image 261
Stella Avatar asked Feb 02 '16 14:02

Stella


People also ask

How do you change the size of a picture on Picasso?

resize(screenWidth, imageHeight) . centerInside() . into(imageView);

How do you change the size of a picture on Picasso Android?

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.

How will you load an image into an imageView from an image URL using Picasso?

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.


4 Answers

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

like image 146
null pointer Avatar answered Oct 11 '22 14:10

null pointer


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:

  • LRU memory cache of 15% the available application RAM
  • Disk cache of 2% storage space up to 50MB but no less than 5MB.
  • Picasso will use OkHttp as the Http client if it's included as a Gradle dependency. Otherwise Picasso will fall back to HttpUrlConnection.

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

  • MemoryPolicy.NO_CACHE: The image won't be served from memory. This one doesn't avoid to serve images from disk. To avoid disk look down on Network Policies.
  • MemoryPolicy.NO_STORE: The image won't be stored in memory. Use this for images that will be loaded only once.

Example

Picasso
        .with(context)
        .load(https://goo.gl/v9EkbF)
        .memoryPolicy(MemoryPolicy.NO_CACHE, MemoryPolicy.NO_STORE)
        .into(imageView);

Network Policies

  • NetworkPolicy.NO_CACHE: The image won't be served from disk cache.
  • NetworkPolicy.OFFLINE: The image will (if possible) be served just from cache (memory or disk) but never from network.

Example:

Picasso
        .with(context)
        .load(https://goo.gl/v9EkbF)
        .networkPolicy(NetworkPolicy.NO_CACHE)
        .into(imageView);

Memory and caching considerations

  • Glide uses by default RGB565 and cache a image resized into the memory.
  • Picasso uses by default RGB888 and cache the full size image into the memory and let GPU does the real-time resizing when drawn.

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

  • Glide caches separate file for each size. If you load another size of the same image, it will be downloaded again before, resized and then cached.
  • Picasso takes always the full-size image from cache and then let the GPU make the resizing into the view.

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

like image 27
Sotti Avatar answered Oct 11 '22 13:10

Sotti


You can use OkHttp from the same vendor "SquareUp".
This answer show you how to do it with details.

like image 3
Kevin Robatel Avatar answered Oct 11 '22 12:10

Kevin Robatel


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.


like image 3
ʍѳђઽ૯ท Avatar answered Oct 11 '22 14:10

ʍѳђઽ૯ท