Skip to content
Related Articles
Open in App
Not now

Related Articles

How to Load Any Image From URL Without Using Any Dependency in Android?

Improve Article
Save Article
  • Last Updated : 05 Aug, 2021
Improve Article
Save Article

Many applications display images from the internet using third-party APIs like Glide and Picasso to load images. This means that such applications partly depend on these services to keep themselves working fine. To make the application better, one should write their own code rather than depending on such services. In this article, we will show you how you can easily load images in your application without using any external service.

Step by Step Implementation

Step 1: Create a New Project in Android Studio

To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. We demonstrated the application in Kotlin, so make sure you select Kotlin as the primary language while creating a New Project.

Step 2: Add Internet permission in the AndroidManifest.xml

Since images are in the form of URLs, the application will need internet permissions to parse the information.


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

Step 3: Create an ImageView in the layout

Navigate to the app > res > layout > activity_main.xml and add the below code to that file. Below is the code for the activity_main.xml file. 


<?xml version="1.0" encoding="utf-8"?>
      <!-- The image will load in this ImageView -->

Step 4: Program to load image from URL and display in the ImageView

Go to the MainActivity.kt file and refer to the following code. Below is the code for the MainActivity.kt file. Comments are added inside the code to understand the code in more detail.


import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.widget.ImageView
import android.widget.Toast
import java.util.concurrent.Executors
class MainActivity : AppCompatActivity(){
    override fun onCreate(savedInstanceState: Bundle?) {
        // Declaring and initializing the ImageView
        val imageView = findViewById<ImageView>(
        // Declaring executor to parse the URL
        val executor = Executors.newSingleThreadExecutor()
        // Once the executor parses the URL 
        // and receives the image, handler will load it
        // in the ImageView
        val handler = Handler(Looper.getMainLooper())
        // Initializing the image
        var image: Bitmap? = null
        // Only for Background process (can take time depending on the Internet speed)
        executor.execute {
             // Image URL
              // Tries to get the image and post it in the ImageView
              // with the help of Handler
              try {
                val `in` =
                image = BitmapFactory.decodeStream(`in`)
                // Only for making changes in UI
            // If the URL doesnot point to 
              // image or any other kind of failure
            catch (e: Exception) {


We can see that the image loads successfully. This means that the application works perfectly as intended.

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!