In this tutorial we will show how to use picasso library in Android. Picasso is open source and widely used image downloader library in android.
There are various methods for downloading images from server . From which Picasso library is widely used for this purpose.It is easy to use.
How to use ?
By the use of only single line code you can download any number of images from server.
Step :1 Firstly Download the Picasso JAR file, If you haven’t done it already. If you are using eclipse as your development IDE, then just copy the downloaded picasso-2.4.0.jar
file into your application lib folder. If you are using Android Studio IDE, then you have to add below dependency in build.gradle file.
dependencies { … compile “com.squareup.picasso:picasso:2.4.0” … }
Step :2 Loading Remote Image in ImageView We are done with configuration, let us see how to use this library to download images from remote server and display in ImageView. I assume you have your activity layout file already with ImageView declared on it.
<ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/imageView" android:layout_alignParentTop="true" android:layout_centerHorizontal="true"> </ImageView>
Step :3 For Picasso download image from server, you need to add below internet permissions to your project’s manifest.
<uses-permission android:name=“android.permission.INTERNET” />
Step :4 Now let us download the image and display on imageView
<ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/imageView" android:layout_alignParentTop="true" android:layout_centerHorizontal="true"> </ImageView>
In the first line we are getting ImageView instance from the layout. And then load the image from the above remote url using Picasso library.
Placeholder ,Error Fallback Image , Resize and Transformation
Picasso offers much more than just downloading image. It can resize the image, transform before it is displayed in ImageView.
Picasso.with(this) .load("YOUR IMAGE URL HERE") .placeholder(R.drawable.ic_placeholder) // optional .error(R.drawable.ic_error_fallback) // optional .resize(250, 200) // optional .rotate(90) // optional .into(imageView);