This tutorial is about loading an image from a URL or the internet in an Android application. Loading an image from a URL can block our main thread, so we use AsyncTask which uses another thread apart from the main thread to do intensive operations.

Below you can find the steps to follow when there is a need to load an image from a URL.

  1. Create an ImageView
    It helps to hold the image which we want to load from a URL.
  2. Create an AsyncTask
    AsyncTask processes the request in another thread. So network intensive operations like loading an image from the internet should be done in a separate thread.
  3. Then we send and receive the request from a URL.
  4. Load the image inside an ImageView.

Screenshot of the application


Let’s now request the image from the server/URL.

Load image from a URL in Android

Below you can find the image which we are requesting from a URL, from our Android application.


Inside the doInBackground method of the AsyncTask, we request the data from a URL.

  • Create a new URL object by passing the location of an image.
  • Then get the data by opening a connection.
  • We get the data in the form of an input stream.
  • Convert the data to a bitmap.

Below you can find the source code of an Android application which loads an image from a URL:

Inside the MainActivity, we have created an ImageView in order to hold the image data. Then we request the image using another thread by executing AsyncTask. When the image data is received from a URL, the control goes to the onPostExecute method of AsyncTask. This method helps to set the image inside the ImageView.

You can find the layout file below.

Make sure to add an internet permission inside your Android manifest file:

I hope you have successfully loaded the image from a URL inside your Android application. In case you have any doubts or suggestions, feel free post comment below. Thank You for reading.