Picasso is a powerful image downloading and caching library for Android.Images add much-needed context and visual flair to Android applications. Picasso allows for hassle-free image loading in your application—often in one line of code!
Lets see how to do.
First download the Picasso jar file from here.
If you're using Android Studio, then you can add compile 'com.squareup.picasso:picasso:2.3.3' to the build.gradle file in the dependency section.
Just add this import statement to your java class.
import com.squareup.picasso.Picasso;
For Picasso to do its work, make sure to add
<uses-permission android:name="android.permission.INTERNET" /> to your project's manifest.
Add imageView to the Layout file.
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageView"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
Add Picasso code:-
ImageView imageView = (ImageView) findViewById(R.id.imageView);
Picasso.with(this)
.load(your_image_url)
.into(imageView);
In the first line, we get a reference to the ImageView instance in the layout file. We then load an image into the image view using the Picasso library. We first specify the context by calling with and passing in the context. We then call the load method and supply it with the location of the image, a URL in this case. Finally, we tell Picasso where it should display the image when it's fetched by calling into and pass in the imageView object.
You can resize your image using,
Picasso.with(this)
.load(https://cms-assets.tutsplus.com/uploads/users/21/posts/19431/featured_image/CodeFeature.jpg)
.resize(100, 100)
.into(imageView)
Picasso also supports transformations, such as rotation. In the next code snippet, we fetch a remote image and rotate it 180 degrees before displaying it in an image view.
Picasso.with(this)
.load("https://cms-assets.tutsplus.com/uploads/users/21/posts/19431/featured_image/CodeFeature.jpg")
.rotate(180)
.into(imageView);
If your application relies on remote assets, then it's important to add a fallback in the form of a placeholder image. The placeholder image is shown immediately and replaced by the remote image when Picasso has finished fetching it.
Picasso.with(this)
.load(https://cms-assets.tutsplus.com/uploads/users/21/posts/19431/featured_image/CodeFeature.jpg)
.placeholder(R.drawable.image_name)
.into(imageView);
Picasso supports two types of placeholder images. We already saw how the placeholder method works, but there's also an error method that accepts a placeholder image. Picasso will try to download the remote image three times and display the error placeholder image if it was unable to fetch the remote asset.
Picasso.with(this)
.load(https://cms-assets.tutsplus.com/uploads/users/21/posts/19431/featured_image/CodeFeature.jpg)
.error(R.drawable.image_name)
.into(imageView);
Note that you can combine the placeholder and error methods as shown in the following code block.
Picasso.with(this)
.load(https://cms-assets.tutsplus.com/uploads/users/21/posts/19431/featured_image/CodeFeature.jpg)
.placeholder(R.drawable.image_name_default)
.error(R.drawable.image_name_error)
.into(imageView);
Lets see how to do.
First download the Picasso jar file from here.
If you're using Android Studio, then you can add compile 'com.squareup.picasso:picasso:2.3.3' to the build.gradle file in the dependency section.
Just add this import statement to your java class.
import com.squareup.picasso.Picasso;
For Picasso to do its work, make sure to add
<uses-permission android:name="android.permission.INTERNET" /> to your project's manifest.
Add imageView to the Layout file.
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageView"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
Add Picasso code:-
ImageView imageView = (ImageView) findViewById(R.id.imageView);
Picasso.with(this)
.load(your_image_url)
.into(imageView);
In the first line, we get a reference to the ImageView instance in the layout file. We then load an image into the image view using the Picasso library. We first specify the context by calling with and passing in the context. We then call the load method and supply it with the location of the image, a URL in this case. Finally, we tell Picasso where it should display the image when it's fetched by calling into and pass in the imageView object.
You can resize your image using,
Picasso.with(this)
.load(https://cms-assets.tutsplus.com/uploads/users/21/posts/19431/featured_image/CodeFeature.jpg)
.resize(100, 100)
.into(imageView)
Picasso also supports transformations, such as rotation. In the next code snippet, we fetch a remote image and rotate it 180 degrees before displaying it in an image view.
Picasso.with(this)
.load("https://cms-assets.tutsplus.com/uploads/users/21/posts/19431/featured_image/CodeFeature.jpg")
.rotate(180)
.into(imageView);
If your application relies on remote assets, then it's important to add a fallback in the form of a placeholder image. The placeholder image is shown immediately and replaced by the remote image when Picasso has finished fetching it.
Picasso.with(this)
.load(https://cms-assets.tutsplus.com/uploads/users/21/posts/19431/featured_image/CodeFeature.jpg)
.placeholder(R.drawable.image_name)
.into(imageView);
Picasso supports two types of placeholder images. We already saw how the placeholder method works, but there's also an error method that accepts a placeholder image. Picasso will try to download the remote image three times and display the error placeholder image if it was unable to fetch the remote asset.
Picasso.with(this)
.load(https://cms-assets.tutsplus.com/uploads/users/21/posts/19431/featured_image/CodeFeature.jpg)
.error(R.drawable.image_name)
.into(imageView);
Note that you can combine the placeholder and error methods as shown in the following code block.
Picasso.with(this)
.load(https://cms-assets.tutsplus.com/uploads/users/21/posts/19431/featured_image/CodeFeature.jpg)
.placeholder(R.drawable.image_name_default)
.error(R.drawable.image_name_error)
.into(imageView);