Downloading An Image From The Server And Displaying It On Screen

This is a sample activity which shows how to get image files from the web and display them using the ImageView view.

Underlying Algorithm:

Basic description of algorithm in step by step form:
1.) Create a Project downloadImage.
2.) Add the relevant permissions to your AndroidManifest.xml file:

 

<!--?xml version="1.0" encoding="utf-8"?-->
package="com.sample.downloadImage"
android:versionCode="1"
android:versionName="1.0.0">

android:label="@string/app_name">
android:label="@string/app_name">

 

 

3.) Define a helper function called OpenHttpConnection() in your activity that opens a connection to a HTTP server and returns an InputStream object:

 

private InputStream OpenHttpConnection(String urlString) throws IOException
{
InputStream in = null;
int response = -1;

URL url = new URL(urlString);
URLConnection conn = url.openConnection();

if (!(conn instanceof HttpURLConnection)) throw new IOException("Not an HTTP connection");

try{
HttpURLConnection httpConn = (HttpURLConnection) conn;
httpConn.setAllowUserInteraction(false);
httpConn.setInstanceFollowRedirects(true);
httpConn.setRequestMethod("GET");
httpConn.connect();

response = httpConn.getResponseCode();
if (response == HttpURLConnection.HTTP_OK) {
in = httpConn.getInputStream();
}
}
catch (Exception ex)
{
throw new IOException("Error connecting");
}
return in;
}

 

 

4.) Replace main.xml with the following code:

 

<!--?xml version="1.0" encoding="utf-8"?-->
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">

android:layout_height="wrap_content" android:layout_gravity="center" />

 

 

5.) Run the application.

Steps to Create:

1.) Open Eclipse. Use the New Project Wizard and select Android Project Give the respective project name i.e. downloadImage. Enter following information:
Project name: downloadImage
Build Target: Google APIs
Application name: downloadImage
Package name: com.sample.downloadImage
Create Activity: downloadImage

On Clicking Finish downloadImage code structure is generated with the necessary Android Packages being imported along with downloadImage.java. downloadImage class will look like following:

 

package com.sample.downloadImage;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.widget.ImageView;

public class downloadImage extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

Bitmap bitmap = DownloadImage(
"http://www.allindiaflorist.com/imgs/arrangemen4.jpg");
ImageView img = (ImageView) findViewById(R.id.img);
img.setImageBitmap(bitmap);
}

private InputStream OpenHttpConnection(String urlString)
throws IOException
{
InputStream in = null;
int response = -1;

URL url = new URL(urlString);
URLConnection conn = url.openConnection();

if (!(conn instanceof HttpURLConnection))
throw new IOException("Not an HTTP connection");

try{
HttpURLConnection httpConn = (HttpURLConnection) conn;
httpConn.setAllowUserInteraction(false);
httpConn.setInstanceFollowRedirects(true);
httpConn.setRequestMethod("GET");
httpConn.connect();

response = httpConn.getResponseCode();
if (response == HttpURLConnection.HTTP_OK) {
in = httpConn.getInputStream();
}
}
catch (Exception ex)
{
throw new IOException("Error connecting");
}
return in;
}
private Bitmap DownloadImage(String URL)
{
Bitmap bitmap = null;
InputStream in = null;
try {
in = OpenHttpConnection(URL);
bitmap = BitmapFactory.decodeStream(in);
in.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
return bitmap;
}
}

 

 

Output –The final output:

Leave a Comment:

16 comments
pdf ebooks free says November 5, 2011

A person necessarily lend a hand to make severely articles I would state. This is the very first time I frequented your website page and so far? I surprised with the research you made to make this particular post amazing. Excellent job!

Reply
Ruchi says November 17, 2011

It is a nice tutorial. I was really really needing this. Thanks a lot.

Reply
AndroidResearch says January 19, 2012

I got a bit confused by the thumbnail of post, where a progress bar is shown. I walked through the code and didn’t find any relevant code using the progress bar.
It will be nice to have a such example.

Reply
aashu says January 30, 2012

Thats really helpful…..Thanks

Reply
prashant says April 17, 2012

thank you ,
the code worked.
made our work easy

Reply
BunnyChow says April 18, 2012

Very nice indeed… I found a script which is also quite useful. It supports image threaded downloading and cache. It is worth at least having a look at it.

http://theandroidcoder.com/utilities/android-image-download-and-caching/

Reply
Meena says July 12, 2012

Am very new to this Android please help me , how should i create http connection in android?And can i know what are the basics should i know it for http connection?

Reply
rajalakshmi says December 7, 2012

Your code is working good… Thanks for your tutorial. This tutorial helps me lot.

Reply
bharath says January 24, 2013

Good post

Reply
jay says March 18, 2013

Nice ..Very clear n understanding code thnx……….

Reply
enrico says March 19, 2013

Thank you, very useful.
And is it possible save the downloaded image to the device?

Reply
jhon says May 4, 2013

If the server asks me username and password. The server is installed on my Pc (xampp)

Reply
Perry says June 29, 2013

Just tried this code, and it ran but did not display an image, I have tried other images but does not show?

Have you any ideas?

Cheer

Reply
Usman says July 30, 2013

Does it work for big images like images of size in MB ?

Reply
Amit says March 11, 2015

response = httpConn.getResponseCode(); is not working.it is throwing exception.
please help me.

Reply
harsha says April 18, 2015

Your code is clear.How can i get the image name on image for that how can i get the image name from the url from http://www.allindiaflorist.com/imgs/arrangemen4.jpg this link i need only image name as arrangemen4

Reply
Add Your Reply