Last Updated:

Picasso Library  - Download and cache images on Android

Picasso Library

In this article, we'll take a look at the Picasso library, one of the most popular and convenient solutions for downloading and caching images on Android.

Picasso Library Overview

First of all, let's see why we need a library at all in order to just download an image. I would give the answer to this question in just three words: Simplicity, Convenience, Optimization.

  • The simplicity of using Picasso is that you can download the image in 2 lines of code.
  • The convenience is that we don't have to think about the possible problems and pitfalls associated with uploading an image.
  • Optimization – Rational use of memory, caching, image transformations, the ability to cancel the download and much more.

All 3 words are closely related and are essentially derived from each other, so just replace them with Picasso 🙂.

The library is the brainchild of the Square open source development team, who created such masterpieces as Retrofit, Dagger, Leakcanary and others.

Picasso Functionality

You can add a library to your project in the dependency block of your file:build.gradle

Then, synchronize the project so that Gradle can load the new dependency into the cache. Make sure your Gradle is not set to work offline.

Uploading an image from the link

To download a picture by URL using Picasso, we must have an ImageView widget (we will load an image into it) or a special Target object (for storing the downloaded object in the form of a Bitmap) is described - this method will be considered at the end of the article.

A simple example of uploading a picture from the link:

In the code above, Picasso gets the current context: with(context), downloads the image: load("link address") and sets it to the target widget: into(imageView)."

Picasso also supports uploading images from assets, assets files and content providers:

 

Error handling and temporal image in Picasso

  • We download the image from the Internet, hence the process will take some time depending on the speed of the Internet. Therefore, a good solution is to show the user a temporary image (placeholder) until the picture is loaded.
  • The Picasso library also takes care of errors related to image uploads. We just specify which image to use if you failed to download the image from the link.

Let's look at the example of error handling and placement holder installation:

Picasso.with(context)
.load("image link here")
.placeholder(R.drawable.placeholder) // show something until the specified image is loaded
.error(R.drawable.error) // show something if image download failed
.into(imageView);

Transformations in Picasso: bothdimensioning and image rotation

 

Picasso also supports cropping the picture in the center, for example:centerCrop()

 

Save the image as a Bitmap

Picasso also has the ability to download a picture not in ImageView, but in the Target object and continue to work with the image in the form of a Bitmap:

In the method, Picasso loads the image into a Target object. It implements a method - in it we can work with a picture in the form of .

loadImage()onBitmapLoaded()Bitmap

  • The advantages of the Picasso library also include the lightness and excellent optimization of memory when loading images.
  • Well, the disadvantage of Picasso is the lack of support for GIF-animations.

These are not all the features of Picasso. More information can be found on the library's GitHub page.

Subscribe to new articles on Java programming and development for Android!