Android Load Image from URL

Android Load Image from URL

In this android code tutorial, we are going to learn how to load image from a URL or internet in android. There are situations when you may need to download an image using the url path of the image in your android application.

Android networking programming offers different APIs and class that can help us achieve this task.

It is important to note that when you are working with internet or accessing data from a remote source in android, you will need to add internet permission in your android manifest file.

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

In our example implementation, we are going to use an AsyncTask class to shown to the first example. The use of Async class is very important since it prevents us from downloading our remote image using the main application Thread.

Let start first with the AsyncTask class.

Before we start, the first thing I will do is to list the environment and tools I used in this android code snippet but feel free to use whatever environment or tools you are familiar with.

Windows 7

Android Studio

Samsung Galaxy Fame Lite

Min SDK 14

Target SDK 19

To create a new android application project, following the steps as stipulated below.

Go to File menu

Click on New menu

Click on Android Application

Enter Project name: AndroidLoadImageFromUrl

Package: com.inducesmile.androidloadimagefromurl

Keep other default selections.

Continue to click on next button until Finish button is active, then click on Finish Button

Once you are done with creating your project, make sure you change the package name if you did not use the same package.

Main Layout

We will start by adding an ImageView in our activity_main.xml layout file. If you are using Eclipse or Android Studio, you can switch to the design view and drag and drop this View inside your layout file.

You can also copy and paste the following code below into the file if you don’t want to do it yourself.

<RelativeLayout xmlns:android=""


android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">


android:layout_centerHorizontal="true" />


Open the Mainfest file and paste the code below

<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android=""
package="" >
<uses-permission android:name="android.permission.INTERNET"/>
android:theme="@style/AppTheme" >

android:label="@string/app_name" >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />


The bold line in the code is what we have added. As I said before, your application might not work without this permission.

Open the file now, we are going to create an Async class as an inner class in our MainActivity class. An object of an ImageView is passed as a parameter to the execute method of the Async class. The complete code for the file is shown below. You can copy and paste it on your own project file.


import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ImageView;

public class MainActivity extends ActionBarActivity {

protected void onCreate(Bundle savedInstanceState) {
ImageView bindImage = (ImageView)findViewById(;
String pathToFile = "";
DownloadImageWithURLTask downloadTask = new DownloadImageWithURLTask(bindImage);

public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(, menu);
return true;

public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == {
return true;
return super.onOptionsItemSelected(item);
private class DownloadImageWithURLTask extends AsyncTask<String, Void, Bitmap> {
ImageView bmImage;
public DownloadImageWithURLTask(ImageView bmImage) {
this.bmImage = bmImage;

protected Bitmap doInBackground(String... urls) {
String pathToFile = urls[0];
Bitmap bitmap = null;
try {
InputStream in = new;
bitmap = BitmapFactory.decodeStream(in);
} catch (Exception e) {
Log.e("Error", e.getMessage());
return bitmap;
protected void onPostExecute(Bitmap result) {

Save the file and run your project. If everything works for you, this image will appear on your test device.

Android Load Image from URL

You can download the code for this tutorial below. If you are having hard time downloading the tutorials, kindly contact me.

Remember to subscribe with your email so that you will be among the first to receive our new post once it is published.

Add a Comment