Hello everyone..
In one of my tutorials I have shown you how to upload an image in android..
In todays tutorial I will show you how to download an image into your phone programatically.
I am just picking up an image url from google to show the download.

Previously I have shown three other methods to upload files to a server.
Check these posts to refer this.

1. Uploading audio, video or image files from Android to server
2. How to Upload Multiple files in one request along with other string parameters in android?
3. ANDROID – Upload an image to a server.

This is the main program that downloads the file.

package pack.coderzheaven;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import org.apache.http.util.ByteArrayBuffer;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class DownloadImage extends Activity {

	private final String PATH = "/data/data/pack.coderzheaven/";
	TextView tv;
    public void onCreate(Bundle savedInstanceState) {
        tv = (TextView)findViewById(R.id.tv);
    public void DownloadFromUrl(String fileName) {
            try {
                    URL url = new URL("http://t3.gstatic.com/images?q=tbn:ANd9GcQs0EPegqi56Alq4vCgC_lVDbZvJtk51RhER7AyDEVA3nUkzjMVK-yDHY3V-w"); //you can write here any link
                    File file = new File(fileName);

                    long startTime = System.currentTimeMillis();
                    tv.setText("Starting download......from " + url);
                    URLConnection ucon = url.openConnection();
                    InputStream is = ucon.getInputStream();
                    BufferedInputStream bis = new BufferedInputStream(is);
                     * Read bytes to the Buffer until there is nothing more to read(-1).
                    ByteArrayBuffer baf = new ByteArrayBuffer(50);
                    int current = 0;
                    while ((current = bis.read()) != -1) {
                            baf.append((byte) current);

                    FileOutputStream fos = new FileOutputStream(file);
                    tv.setText("Download Completed in" + ((System.currentTimeMillis() - startTime) / 1000) + " sec");
            } catch (IOException e) {
            	 tv.setText("Error: " + e);


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"

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

    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".DownloadImage"
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />


<?xml version="1.0" encoding="utf-8"?>
    <string name="hello">DownloadImage Demo from CoderzHeaven</string>
    <string name="app_name">DownloadImage</string>
Download Image Demo

Download Image Demo

Download Image Demo

Download Image Demo



  1. Pingback: Link to How to Download an image in ANDROID programatically? | Coderz Heaven

  2. limo July 8, 2011 at 12:56 pm

    well cool stuff and only people who would be able to do this are programmers, so what would you advice for non-programmers as myself!!

  3. Android Applications Development July 8, 2011 at 3:22 pm

    Thanks for sharing in details about “how to upload an image in android”.This will be very useful for the Android users.

  4. ishan July 8, 2011 at 5:50 pm

    how to populate a list view with images on my sqlite database
    (using image paths from sdcard)

    • James July 8, 2011 at 10:06 pm

      ishan : We only reply to people who are there in our Facebook or twitter Fan club. Sorry…

  5. kien July 13, 2011 at 9:08 am

    I don’t run the program.I wrote than you
    It recieve the “try catch” as soom as the program runs

  6. Mateus July 18, 2011 at 10:46 pm

    Hi Dear! Excellent job!
    I’m trying download files 2, 3 and 8MB each (many files…). When I type on adb shell ls -l, all files has been listed, but, all the same size :-S. Can you help me?

    • James July 18, 2011 at 11:19 pm

      Hi Mateus:- Recheck your code, this will not happen, Are you downloading the same file with different names. Open the images and see.

  7. LamprosGk December 21, 2011 at 7:18 pm

    Hi, I want to download all images that exist in a specific folder in my server, do I a need server-side/php script also or can I do that only with java in my app code?

    • James December 21, 2011 at 10:03 pm

      NO need of a php script actually but you need to know the URL of the images in your server.
      OR another way is write a php script to send the URLs and get it on the android side then start downloading images one by one.

      • LamprosGk December 22, 2011 at 2:38 am

        thanks James.
        so the use of a server-side script is only to get the URLs, the transfer gets done by java right?
        Assuming i know the URLs, there’s no need of script.

        (forgive me for my not so good english :p)

        • James December 22, 2011 at 10:57 pm

          yes, java can do the download, check this post and replace with your image URL.IF you know the image URLs then there is no need of the script.

  8. LamprosGk December 22, 2011 at 11:26 pm

    thanx a lot James! 😉

  9. jatinkumar January 11, 2012 at 5:55 pm

    Thank You

  10. Jimsonmok February 1, 2012 at 6:07 pm

    Besides download an image, how to download an audio from server to an android phone?

    • James February 1, 2012 at 8:37 pm

      Hello Jimsonmok:-
      Downloading an audio file is same as downloading an image file, just the extension changes.

  11. Pingback: Download Image From URL in Android « tediscript.wordpress.com

  12. sheelu March 13, 2013 at 6:47 am

    When the code is copied and executed..
    there was an error occuring.. the field tv missing in R.id..

    How to edit this??


Leave a Reply

Your email address will not be published. Required fields are marked *




Theme by HermesThemes

Copyright © 2018 CoderzHeaven. All Rights Reserved

Please wait...

Subscribe to our newsletter

Want to be notified when our article is published? Enter your email address and name below to be the first to know.