How To: Getting Started With Picasso

Picasso is a widely used open source image loading library from Square. It is well tested, provides a number of features and is fast and optimized.

Step 1:

To get started add Picasso to your project dependency. Add the following lines to build.gradle file. Please check the latest version here and make the necessary adjustments.

dependencies {
    // your app's other dependencies
    compile 'com.squareup.picasso:picasso:2.5.2'
}

Step 2:

Give internet permissions to your application if the image is on a remote location. Add the following lines to AndroidManifest.xml file.

<manifest
    ... >
    // this line
    <uses-permission android:name="android.permission.INTERNET" />
    <application
        ...
        android:label="@string/app_name"
        android:name=".MyApplication">
        ...
    </application>
    ...
</manifest>

Step 3:

Add the following lines to the layout.xml file.

<ImageView
    android:id="@+id/my_image_view"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@mipmap/ic_launcher"
/>

Step 4:

Finally, add the following lines to the Activity.java file

ImageView imageView = (ImageView) findViewById(R.id.my_image_view);
Picasso.with(context)
    .load(url)
    .placeholder(R.drawable.placeholder)
    .error(R.drawable.placeholder_error)
    .into(imageView);

Placeholder is the image that will be displayed while the requested image is being loaded and error is the image that will be displayed if an error occurs in getting the requested image resource. Both placeholder and error are optional and can be excluded.

Step 5:

That is all, now build and run the application. The image will be downloaded, cached, displayed, and cleared from memory when your view goes off-screen.

If there are any questions let me know in the comments below. Check out the HowTo for Glide and Fresco as well.

Show Comments