Picasso Android Tutorial – Android Picasso Image Loader Library

Hello friends, today we will be learning about how we can use picasso android library developed by sqaure. Picasso one of the most popular library for android. It is very simple and powerful library for image downloading and …

picasso android tutorial

Hello friends, today we will be learning about how we can use picasso android library developed by sqaure. Picasso one of the most popular library for android. It is very simple and powerful library for image downloading and caching.

There are also some alternative libraries like Volley. I already a published a tutorial about Using Android Volley Library to Load Image From Internet. In this post we will learn about picasso android library.

Why Picasso Android or Any other Android Library?

You might be thinking that why we should use a 3rd party library. You can achieve your task without using a 3rd party API as well. I have also posted a tutorial about downloading image without using 3rd party library. But if you will use the core method then it would take larger amount of code. But if we will use a 3rd party library like picasso then we will achieve our goal in few lines of code.  So if we will not use a 3rd party library then  we would need

  1. Very large amount of code to be written
  2. We have to write another logic to implement caching. Caching is very important to make the application faster.
  3. We also have to deal with memory while writing the code.

BUT if we will use picasso then all the above mentioned things would be taken care of by picasso.

Adding Picasso Library to our Android Project

Adding picasso android library to your project is very easy. You just need to add the following line in the dependency block of your build.gradle file. I am assuming that you are using Android Studio.

After adding it just sync your project.

Loading Image from URL by Using Picasso Android Library

Loading image from URL by using Picasso Android Library is very simple and easy. The first thing we would need is an ImageView. Unlike Volley’s NetworkImageView with Picasso we can use normal ImageView.

Code for Loading Image with Picasso

It is very simple. We have to use the Picasso class.

Placeholder and Error Handling

Because we are loading the image from internet; the process would take some time depending on the internet speed. So it would be a good idea to display a image from the device while the image from URL is getting loaded.

One more situation could be when the image is not downloaded from the URL (when the URL given is wrong). In this case we should display an error image. Both these things can be done very easily by using picasso. See the following code snippet.

Re-sizing and Rotating

We can also resize and rotate the image very easily.

Trying Picasso Android Library in Our Project

Now lets try the above codes in our Android Studio Project. So I will be creating a new Android Project.

  • Open Android Studio and create a new project. I have created PicassoDemo.
  • First we have to add the Picasso Library. So open your build.gradle and add the following line inside dependency block and sync your project.

  • As we will load the image from a URL so we will also need internet permission. So open AndroidManifest.xml and add internet permission.

  • We will create an ImageView now. So come inside activity_main.xml and write the following xml code.

  • Now come inside MainActivity.java and define your ImageView.

  • We need two images one for placeholder and one for error. I will be using the following images. Just save these and paste inside the drawable folder of your project.
error
error
placeholder
placeholder
  • Now we need a URL to an ImageFile. I have this URL

https://www.simplifiedcoding.net/wp-content/uploads/2015/10/advertise.png

  • We will load the image from the above URL. Write the following code inside onCreate().

  • So the final code for our MainActivity.java would be.

  • Finally run your application.
picass android
Picasso Android
  • Bingo! Its working absolutely fine.

So thats all for this Picasso Android Tutorial friends. Feel free to ask if having any query regarding this Picasso Android tutorial. Thank You 🙂

Hi, my name is Belal Khan and I am a Google Developers Expert (GDE) for Android. The passion of teaching made me create this blog. If you are an Android Developer, or you are learning about Android Development, then I can help you a lot with Simplified Coding.

Expand Your Knowledge: Next Tutorial Picks

5 1 vote
Article Rating
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x