Friday , October 22 2021

Picasso Android Tutorial – Android Picasso Image Loader Library – Android Coding by DMTechnolab

Hello friends, today we will learn how we can use picasso android Library developed by Trap. Picasso one of the most popular library for Android. It is a very simple and powerful library for image downloading and caching.

There are also some alternative libraries such as Volley. I have already published a tutorial about Using Android volley library to load image from internet. In this post we will know picasso android library.

Why Picasso Android or any other Android library?

You may be wondering why we should use 3rd party library. You can also get your work done without using third party APIs. I have also posted a tutorial Image download without using 3rd party library. But if you use core method then it will take a large amount of code. But if we use the Three Party Library like Picasso then we will achieve our goal in a few lines of code. So if we don’t use 3rd party library then we will need

  1. Writing large amounts of code
  2. We need to write another argument to implement caching. Caching is very important to make the application faster.
  3. We also have to deal with memory while writing code.

But if we use Picasso then all of the above will be taken care of by Picasso.

Add the Picasso library to our Android project

It is very easy to add picasso android library to your project. You just need to add the following line to the dependency block of your build. Gradle file. I’m assuming you’re using Android Studio.

Just sync your project after adding it.

Picasso is loading image from URL using Android library

Loading an image from a URL is very simple and easy using the Picasso Android library. The first thing we need is a ImageView. Opposite volley NetworkImageView With Picasso we can use normal ImageView.

Code for loading image with Picasso

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

Placeholder and error handling

Because we are loading the image from the Internet; Depending on the speed of the Internet, the process will take some time. So it would be good to display an image from the device while the image is being loaded from the URL.

Another situation can occur when the image is not downloaded from the URL (when the URL is incorrect). In this case we should display an error image. Both of these things can be done very easily using Picoso. See the following code snippet.

Resize and rotate

We can resize and rotate the image easily.

In our project Picasso is trying Android library

Now try the above code in our Android Studio project. So I will create a new Android project.

  • Open Android Studio and create a new project. I had made Picasodemo.
  • First we have to add the Picasso library. So open your build.grade and add the following line inside the dependency block and sync your project.
  • As we will load the image from the URL, so we will also need internet permission. Then open AndroidManifest.xml And add Internet permission.
  • We will create one ImageView now. So come in activity_main.xml And write the following xml code.
  • Come in now MainActivity.java And define yourself ImageView.
  • We need two for the placeholder and one for the error. I will use the following images. Just save them and paste them inside the drawable folder of your project.
ErrorError
Error
PlaceholderPlaceholder
Placeholder
  • Now we need a URL for 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 us MainActivity.java Will happen.
  • Finally run your application.
picass androidpicass android
Picasso Android
  • Bingo! It works perfectly.

So for this we have to Picasso Android Tutorial friend. Feel free to ask a question about it Picasso Android Tutorial. Thank you 🙂

Belal Khan ProfileBelal Khan Profile

Hi, my name is Belal Khan and I am Google Developers Specialist (GDE) for Android. A passion for teaching made me this blog. If you are an Android developer, or you are learning about Android development, I can help you a lot with simplified coding.


Source link

About dmtechnolab

Check Also

Simplified Coding

Android Espresso Tutorial – Testing Fragment in Isolation – Android Coding by DMTechnolab

Welcome to another tutorial in the Android test series. And this post is called Android …

2 comments

  1. This post is truly a pleasant one it assists new the web viewers, who are wishing in favor of blogging.| Mariana Ewen Hayden

  2. Pretty! This was a really wonderful article. Thanks for providing this info. Dierdre Mathew Hartfield

Leave a Reply

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