Android Download Image From URL

In this tutorial, you will learn how to download an image from a URL address into your Android application. We will create a button and on button click will start an AsyncTask class to begin downloading an image from a URL address. So lets begin…

Create a new project in Eclipse File > New > Android Application Project. Fill in the details and name your project DownloadImageTutorial.

Application Name : DownloadImageTutorial

Project Name : DownloadImageTutorial

Package Name : com.androidbegin.downloadimagetutorial

Open your MainActivity.java and paste the following code.

MainActivity.java

package com.androidbegin.downloadimagetutorial;

import java.io.InputStream;

import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.app.ProgressDialog;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends Activity {

	// Set your Image URL into a string
	String URL = "https://www.androidbegin.com/wp-content/uploads/2013/07/HD-Logo.gif";
	ImageView image;
	Button button;
	ProgressDialog mProgressDialog;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// Get the layout from image.xml
		setContentView(R.layout.activity_main);

		// Locate the ImageView in activity_main.xml
		image = (ImageView) findViewById(R.id.image);

		// Locate the Button in activity_main.xml
		button = (Button) findViewById(R.id.button);

		// Capture button click
		button.setOnClickListener(new OnClickListener() {
			public void onClick(View arg0) {

				// Execute DownloadImage AsyncTask
				new DownloadImage().execute(URL);
			}
		});
	}

	// DownloadImage AsyncTask
	private class DownloadImage extends AsyncTask<String, Void, Bitmap> {

		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			// Create a progressdialog
			mProgressDialog = new ProgressDialog(MainActivity.this);
			// Set progressdialog title
			mProgressDialog.setTitle("Download Image Tutorial");
			// Set progressdialog message
			mProgressDialog.setMessage("Loading...");
			mProgressDialog.setIndeterminate(false);
			// Show progressdialog
			mProgressDialog.show();
		}

		@Override
		protected Bitmap doInBackground(String... URL) {

			String imageURL = URL[0];

			Bitmap bitmap = null;
			try {
				// Download Image from URL
				InputStream input = new java.net.URL(imageURL).openStream();
				// Decode Bitmap
				bitmap = BitmapFactory.decodeStream(input);
			} catch (Exception e) {
				e.printStackTrace();
			}
			return bitmap;
		}

		@Override
		protected void onPostExecute(Bitmap result) {
			// Set the bitmap into ImageView
			image.setImageBitmap(result);
			// Close progressdialog
			mProgressDialog.dismiss();
		}
	}
}

In this activity, we have created a Button and an ImageView and on button click will show a progress dialog and start the Download Image AsyncTask class. The download image will be decoded into a bitmap and set into an ImageView. In this tutorial, we’ve hosted the sample image GIF file in our server. Click on this link to see the sample image. https://www.androidbegin.com/wp-content/uploads/2013/07/HD-Logo.gif

Next, create an XML file for your MainActivity graphical layout. Go to res > layout > Right Click on layout > New > Android XML File

Name your new XML file activity_main.xml and paste the following code.

activity_main.xml

<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" >

    <ImageView
        android:id="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true" >
    </ImageView>

    <Button
        android:id="@+id/button"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/image"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="@string/button" />

</RelativeLayout>

Next, change the application name and texts. Open your strings.xml in your res > values folder and paste the following code.
strings.xml

<resources>

    <string name="app_name">Download Image Tutorial</string>
    <string name="menu_settings">Settings</string>
    <string name="button">Download Image</string>

</resources>

In your AndroidManifest.xml, we need to declare a permission to connect to the Internet. Open your AndroidManifest.xml and paste the following code.

AndroidManifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.androidbegin.downloadimagetutorial"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="15" />

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

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity android:name=".MainActivity" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

Output :

Download Image Tutorial ScreenShots

 

Source Code 

[purchase_link id=”7878″ text=”Purchase to Download Source Code” style=”button” color=”green”]

Latest comments

I spent 24 hours and finally this article solved my problem!!!! even though i saw it earlier but didnt use it however now i used it and is working as smooth as a code can possibly work!! thanks you very much!!

CheeseBurger (Mohsen Doustkhah

Android Download Image From URL

hi.. what if i want may downloaded files to be save on specific path or folder?

Rechille Ann Basco Denisado

Android Download Image From URL

If i have to call this project from an another activity. How would i go about doing that?

Harminder Singh Deol

Android Download Image From URL

good tutorial!

Khắc Phục

Android Download Image From URL