0
votes

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.

2
you are wrong, it downloads full size images. There is no way to download particular pixels of image.Vladyslav Matviienko
so decodeStream from BitmapFactory and inSampleSize can not be used in InputStream from server?Krzysztof Itaudit

2 Answers

2
votes

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.

1
votes

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.