More Functionalities of the Picasso Library

For any real-time application, we must think of all possible cases. In the above code, we just download the image from the server link. But how to display that image in the app.How to resize it and what if the image loading failed? We need to have another image showing an error message that image loading failed. This all matters for an app developer. The following code changes are made in the MainActivity.java file.

1) Resize

Here we are using Picasso to fetch a remote image and resize it before displaying it in an ImageView.

Java




import androidx.appcompat.app.AppCompatActivity;
  
import android.os.Bundle;
import android.widget.ImageView;
  
import com.squareup.picasso.Picasso;
  
public class MainActivity extends AppCompatActivity {
  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
  
        ImageView imageView = findViewById(R.id.imageView);
        Picasso.with(this)
                .load("https://media.w3wiki.org/wp-content/uploads/20210101144014/gfglogo.png")
                .resize(300, 300)
                .into(imageView);
    }
}


Output:

2) Placeholder 

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.

Java




import androidx.appcompat.app.AppCompatActivity;
  
import android.os.Bundle;
import android.widget.ImageView;
  
import com.squareup.picasso.Picasso;
  
public class MainActivity extends AppCompatActivity {
  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
  
        ImageView imageView = findViewById(R.id.imageView);
        Picasso.with(this)
                .load("https://media.w3wiki.org/wp-content/uploads/20210101144014/gfglogo.png")
                .placeholder(R.mipmap.ic_launcher)
                .into(imageView);
    }
}


Output:

3) Error Fallback 

  • 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.
  • The error image will be shown, in this case when there is no internet connectivity for the application. Instead of loading the image from the URL, the Picasso library shows the error image.

Note: To see this result uninstall the previously loaded application and then install the fresh version of the application from the Android Studio.

Java




import androidx.appcompat.app.AppCompatActivity;
  
import android.os.Bundle;
import android.widget.ImageView;
  
import com.squareup.picasso.Picasso;
  
public class MainActivity extends AppCompatActivity {
  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
  
        ImageView imageView = findViewById(R.id.imageView);
        Picasso.with(this)
                .load("https://media.w3wiki.org/wp-content/uploads/20210101144014/gfglogo.png")
                .error(R.drawable.error_gfg)
                .into(imageView);
    }
}


Output:

4) Cropping 

If you are not sure about the size of the image loaded from the remote server that what will be the size of the image. So in this code snippet image will make the image center cropped.

Java




import androidx.appcompat.app.AppCompatActivity;
  
import android.os.Bundle;
import android.widget.ImageView;
  
import com.squareup.picasso.Picasso;
  
public class MainActivity extends AppCompatActivity {
  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
  
        ImageView imageView = findViewById(R.id.imageView);
        Picasso.with(this)
                .load("https://media.w3wiki.org/wp-content/uploads/20210101144014/gfglogo-300x300.png") // Equivalent of what ends up in onBitmapLoaded
                .placeholder(R.mipmap.ic_launcher)
                .error(R.drawable.error_gfg)
                .centerCrop()
                .fit()
                .into(imageView);
    }
}


Output:

How to Use Picasso Image Loader Library in Android?

Picasso is open source and one of the widely used image download libraries in Android. It is created and maintained by Square. It is among the powerful image download and caching library for Android. Picasso simplifies the process of loading images from external URLs and displays them on your application. For example, downloading an image from the server is one of the most common tasks in any application. And it needs quite a larger amount of code to achieve this via android networking API. By using Picasso, you can achieve this with a few lines of code. 

Similar Reads

How to Use Picasso Android Library?

Step 1: Create an empty activity project...

More Functionalities of the Picasso Library

...

Troubleshooting while loading an image using Picasso Library

...

Contact Us