How to Load Any Image From URL Without Using Any Dependency in Android?
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.
XML
< 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
<? xml version = "1.0" encoding = "utf-8" ?> < RelativeLayout xmlns:android = "http://schemas.android.com/apk/res/android" xmlns:tools = "http://schemas.android.com/tools" android:layout_width = "match_parent" android:layout_height = "match_parent" tools:context = ".MainActivity" > <!-- The image will load in this ImageView --> < ImageView android:id = "@+id/imageView" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:layout_centerInParent = "true" /> </ RelativeLayout > |
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.
Kotlin
import android.graphics.Bitmap import android.graphics.BitmapFactory import android.os.Bundle import android.os.Handler import android.os.Looper import android.widget.ImageView import android.widget.Toast import androidx.appcompat.app.AppCompatActivity import java.util.concurrent.Executors class MainActivity : AppCompatActivity(){ override fun onCreate(savedInstanceState: Bundle?) { super .onCreate(savedInstanceState) setContentView(R.layout.activity_main) // Declaring and initializing the ImageView val imageView = findViewById<ImageView>(R.id.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 val imageURL = "https://media.w3wiki.net/wp-content/cdn-uploads/gfg_200x200-min.png" // Tries to get the image and post it in the ImageView // with the help of Handler try { val `in` = java.net.URL(imageURL).openStream() image = BitmapFactory.decodeStream(`in`) // Only for making changes in UI handler.post { imageView.setImageBitmap(image) } } // If the URL doesnot point to // image or any other kind of failure catch (e: Exception) { e.printStackTrace() } } } } |
Output:
We can see that the image loads successfully. This means that the application works perfectly as intended.