How to Load Images From URL Using Picasso in Android Java

In this article, we will learn how to Load Images from URL in Android Studio using Picasso in Java. At the end of this tutorial, you will see the following screen will be designed.

Load Images From URL Using Picasso

Dependencies

Add the following dependencies in your build.gradle.

implementation 'com.squareup.picasso:picasso:2.5.2'

Sync your project after adding the Picasso.

Add Internet Permission

To load images from the internet we are using the URLs and we also need to include the permission of the INTERNET in our AndroidManifest.xml file.

<uses-permission android:name="android.permission.INTERNET" />

Add the permission when you need to load images from the internet.

URL for Images

We have the URLs of the Images and also the paths where need to display the images.

Picasso.with(MainActivity.this).load(url).into(img);

MainActivity Code

We can load an image by pressing the button and Picasso code is written under setOnClickListener() function.

package com.example.handyopinion;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import com.squareup.picasso.Picasso;


public class MainActivity extends Activity {
    ImageView img;
    Button btn;
    String url ="https://images.unsplash.com/photo-1496065187959-7f07b8353c55?ixid=MXwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHw%3D&ixlib=rb-1.2.1&auto=format&fit=crop&w=750&q=80";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        img = (ImageView)findViewById(R.id.img);
        btn = (Button)findViewById(R.id.btn);

        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Picasso.with(MainActivity.this).load(url).into(img);
            }
        });
    }
}

In this tutorial, we learn how to Load the Image from URLs with Picasso, and we have learned how the library works and how we can integrate it into your project. Picasso documentation.

If you have any questions or suggestions, feel free to ask in the comments section below.

Next Article

1. How to Load Images From URL Using Glide in Android Java
2. How to Load Multiple Images From URL in Android Using Picasso Kotlin

Please share this post:
Posts created 41

Ask a Question

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

Related Posts

Begin typing your search term above and press enter to search. Press ESC to cancel.

%d bloggers like this: