How to Load Images From URL using Glide in Android Java

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

Load Images From URL in Android

Dependencies

Add the following dependencies in your build.gradle.

implementation 'com.github.bumptech.glide:glide:4.12.0'

Make sure you Sync your project after adding the Glide.

Internet Permission

We are loading the images from the URLs with the help of the internet, that why INTERNET permission required.

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

URL for Images

We have the Images URLs and the path where we have to display the images.

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

We have create the XML files and displaying images in the ImageView.

MainActivity Code

In the MainActivity.java the file we can load an image on click button and Glide 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.bumptech.glide.Glide;


public class MainActivity extends Activity {
    ImageView img;
    Button btn;

String url="https://i.picsum.photos/id/197/536/354.jpg?hmac=nVYkcg5S2fYZ85E_Kb43I7VgCxEo0Ltpdh3FjaL6gdw";

    @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) {
                Glide.with(MainActivity.this).load(url).into(img);
            }
        });
    }
}

In this tutorial, we learn how to Load Images from URL with Glide, and we have learned how the library works and how we can integrate it into your project. 

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

Next Article

1. How to Load Multiple Images From URL in Android Using Picasso Kotlin
2. How to Save And Retrieve Class Object From Shared Preferences Using Gson Library in Kotlin Android Studio

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: