Android Picasso Example in Android Studio

Picasso is an image library for Android. It simplifies the process of displaying images from internal storage or from an external URL. When it comes to processing images in an app, Picasso is always my choice.

Loading an image is just simple like this:

[java]

//from URL
Picasso.with(context).load("http://ecx.images-amazon.com/images/I/51E4mxqPUlL._SX331_BO1,204,203,200_.jpg").into(imageView);
//from resources, assets, files, content providers
Picasso.with(context).load(R.drawable.camera).into(imageView);
Picasso.with(context).load("file:///android_asset/file_name.png").into(imageView);
Picasso.with(context).load(new File(…)).into(imageView);

[/java]

This post will show how to develop an app which load image from website in Android Studio.

  1. Create a new project
  2. Import Picasso library to Android Studio
  3. Load image to an ImageView

Create a new project

I only mention my application name and file here to avoid confusion about name in later parts.

Application Name: PicassoApp
Company Domain: com.test.picassoapp

Since I don’t want to complicate this app, I chose “Empty Activity” as default layout.
empty_activity

Import Picasso library to Android Studio

It is kind of simple to import this library in Android Studio. You just need to add compile ‘com.squareup.picasso:picasso:2.5.2’ line to build.gradle file in your project. There is anjother way which download the JAR file and import it to libs folder.

This is how my project’s build.graddle looks like:

[javascript]
apply plugin: ‘com.android.application’

android {
compileSdkVersion 23
buildToolsVersion "23.0.2"

defaultConfig {
applicationId "picassoapp.test.com.picassoapp"
minSdkVersion 15
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile(‘proguard-android.txt’), ‘proguard-rules.pro’
}
}
}

dependencies {
compile fileTree(dir: ‘libs’, include: [‘*.jar’])
testCompile ‘junit:junit:4.12’
compile ‘com.android.support:appcompat-v7:23.3.0’
compile ‘com.squareup.picasso:picasso:2.5.2’
}

[/javascript]

Load image to an ImageView

Firstly, open main activity's layout file (activity_main.xml in my case) and add a new ImageView widget.

[xml]
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="picassoapp.test.com.picassoapp.MainActivity">

<ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/imageView" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" />

</RelativeLayout>
[/xml]

The next step is to download an image and load it to mentioned ImageView using Picasso class.

Add following lines to onCreate method of MainActivity.java:
[java]
ImageView imageView = (ImageView) findViewById(R.id.imageView);

Picasso.with(this)
.load("http://i.imgur.com/ycZFZgV.png")
.into(imageView);
[/java]

Remember to add import com.squareup.picasso.Picasso; for Picasso class to work.

Picasso offers more using features such as image resizing, error handling and placeholder. Here is an example:

[java]
Picasso.with(this)
.load("http://i.imgur.com/ycZFZgV.png")
.resize(100, 100)
.placeholder(R.drawable.image_placeholder)
.error(R.drawable.image_error)
.into(imageView);
[/java]

Since we are loading image from an URL, it is required to add INTERNET permission. Add <uses-permission android:name="android.permission.INTERNET" /> to project's manifest file.

If you don't want to cache, add networkPolicy and memoryPolicy.

[java]
Picasso.with(this)
.load("http://i.imgur.com/ycZFZgV.png")
.networkPolicy(NetworkPolicy.NO_CACHE)
.memoryPolicy(MemoryPolicy.NO_CACHE)
.resize(100, 100)
.placeholder(R.drawable.image_placeholder)
.error(R.drawable.image_error)
.into(imageView);
[/java]

Result:

android_picasso

The library can be downloaded on GitHub.

Leave a Comment

Your email address will not be published. Required fields are marked *


Scroll to Top

By continuing to use the site, you agree to the use of cookies. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.

Close