Using Picasso for Image Loading

Using Picasso for Image Loading

Picasso is a android library used for loading and caching images. It was created by Square. Picasso is a complete library, from making connection to the server for image and loading them in the app, thus reducing the amount of code to be written for the same.

This is a small tutorial on how to integrate Picasso into your app to ease the process of image loading and caching.

Gradle Dependency

Just a single line of code to your dependency block in build.gradle, and you are done

    compile 'com.squareup.picasso:picasso:2.5.2'

Demo Implementation

Start by creating an android project, with an ImageView in your layout file. Add few lines of code to your java file to load images in the imageview using Picasso.

    Picasso.with(context).load("your_image_url").into(imageView);

Caching Images

Many issues such as image caching in memory / disk are all handled by Picasso. You can also provide your own logic of caching images for your app.

So, if you do not want your picasso request to be cached into memory, MemoryPolicy.NO_CACHE will do it for you.

    Picasso  
        .with(context)
        .load("your_image_url")
        .memoryPolicy(MemoryPolicy.NO_CACHE)
        .into(imageView);

Transforming Images

Picasso also helps in transforming images to better fit into your layout.

To reduce the size of a image, we can use resize(width,height) for the same.

    Picasso.with(context)
        .load("your_image_url")
        .resize(50, 50)
        .centerCrop()
        .into(imageView)

Placeholder and Error Images

Picasso provides an optional place holder image that will be displayed until your image is loaded, and error image is displayed when there is any error (network error etc) while loading the image.

    Picasso.with(context)
        .load("your_image_url")
        .placeholder(R.drawable.placeholder)
        .error(R.drawable.error)
        .into(imageView);

This is a very useful library for loading and caching images with out any external implementation.

Development Purpose

For developement purpose ie debug build, you can enable setIndicatorsEnabled(true) which displays a small color band on the top left of image view.

Make sure you disable this option in the release build.

Easy isn’t it ? Just few lines of code ,and you are done. Now, no need to write complex code for caching images. Picasso provides it all.