Tuesday 25 June 2013




Create new Android Project
Project Name: DownloadImageAndDisplay
Build Target: Android 2.3.3   //or older than that
Application Name: Download Image Display
Package Name: ham.android.study
Create Activity: MainActivity
Min SDK: 10 // or greater than that



  1. create main layout:
  • Text view to display the URL of the image.
  • One image view to display the image.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <TextView
        android:id="@+id/tVImageUrl"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
    />
    <ImageView
        android:id="@+id/imView"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
    />
</LinearLayout>

    2. code of main activity:

package com.shaikhhamadali.blogspot.downloadimageanddisplay;

import java.io.InputStream;
import java.net.URL;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.widget.ImageView;
import android.widget.TextView;
public class MainActivity extends Activity {
 // declare internal using controls
 private TextView tVImageUrl;
 private ImageView imView;
 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  // set url text
  String url = "http://i1023.photobucket.com/albums/af358/shaikhhamadali/941861_557664904293238_436756328_n_zps52d3ce7e.jpg";
  tVImageUrl= (TextView)findViewById(R.id.tVImageUrl);

  // load image view control
  imView=(ImageView)findViewById(R.id.imView);
  // grab image to display
  int currentapiVersion = android.os.Build.VERSION.SDK_INT;
  //if OS version is greater than GingerBread
  if (currentapiVersion >= android.os.Build.VERSION_CODES.GINGERBREAD_MR1){
   // Load image in background process for Ginger bread and above versions
   new DownloadImageTask(imView)
   .execute(url);
   tVImageUrl.setText("Welcome to Shaikhhamadali.blogspot.com");
  }
  //if OS version is less than GingerBread
  else{ try {
   imView.setImageDrawable(getImageFromUrl(url));
  } catch(Exception e) {
   tVImageUrl.setText("Error: Exception");
  }
  }
 }
 private Drawable getImageFromUrl(String url) throws Exception {
  return Drawable.createFromStream((InputStream)new URL(url).getContent(), "src");
 }

 private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
  //declare variable of ImageView
  ImageView bmImage;

  public DownloadImageTask(ImageView bmImage) {
   //initialize variable with the passing ImageView parameter
   this.bmImage = bmImage;
  }

  protected Bitmap doInBackground(String... urls) {
   //get url of image
   String urldisplay = urls[0];
   //create Bitmap variable
   Bitmap mIcon11 = null;
   try {
    //create instance of InputStream and pass URL
    InputStream in = new java.net.URL(urldisplay).openStream();
    //decode stream and initialize bitmap 
    mIcon11 = BitmapFactory.decodeStream(in);
   } catch (Exception e) {
    //show error log
    Log.e("Error", e.getMessage());
    e.printStackTrace();
   }
   //return bitmap
   return mIcon11;
  }

  protected void onPostExecute(Bitmap result) {
   //get bitmap and initialize ImageView
   bmImage.setImageBitmap(result);
  }
 }
}
  3. note that:

  • In order to make application to be able to connect to Internet and download files, you need to set uses-permission. Open file: AndroidManifest.xml and add this line:
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
  •  The above code is not the best implementation
  •  Due to just giving an example of getting image, so I don’t care what kind of exception it might catch. You can find it by yourselves, it’s pretty much easy to figure out.
  4. conclusion:
  • Know how to get an image from an URL (without caching file)
  • Know how to get device OS version.
  • know  how to load image using background AsyncTask.
  • Know more about AsyncTask
  5. About the post:

  • The code seems to explain itself due to comments, and is very easy to understand.
  • Don’t mind to write a comment whatever you like to ask, to know,to suggest or recommend.
  • Hope you enjoy it!

  6. Source Code:


        you can download the source code here
Cheers,
Hamad Ali Shaikh
Categories: , , ,