Friday, March 18, 2011

Load Image from server using thread

This is a project where I implemented how to load image form server
Here we have a image URL and need to load that image to a image view

In the layout xml I defined a web view

main.xml

<?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:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:text="@string/hello" />
 <ProgressBar    
   android:id="@+id/progressBar"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content" />
 <ImageView
   android:id="@+id/imageView"
   android:visibility="gone"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"/>
 
</LinearLayout>

Then in my activity i have used a thread (ImageDownlaodThread) to load the image from server. Where the function LoadImageFromWebOperations will read to image to InputStream, this is later converted to Drawable by Drawable.createFromStream

Using the Handler, when the thread convert to drawable it will be set to the image view


Then my main activity ImageDownload.java


import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;

import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.ImageView;
import android.widget.ProgressBar;

public class ImageDownload extends Activity {
 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);

  ImageView mainImageView = (ImageView) findViewById(R.id.imageView);
  ProgressBar progressBar = (ProgressBar) findViewById(R.id.progressBar);
  String imageurl = "http://developer.android.com/assets/images/home/honeycomb-android.png";
  
  ImageDownloadMessageHandler imageDownloadMessageHandler1= new ImageDownloadMessageHandler(progressBar, mainImageView);
  ImageDownlaodThread imageDownlaodThread = new ImageDownlaodThread(imageDownloadMessageHandler1,imageurl);
  imageDownlaodThread.start();
  
 }
 
 class ImageDownlaodThread extends Thread {
  ImageDownloadMessageHandler imageDownloadMessageHandler;
  String imageUrl;

  public ImageDownlaodThread(ImageDownloadMessageHandler imageDownloadMessageHandler, String imageUrl) {
   this.imageDownloadMessageHandler = imageDownloadMessageHandler;
   this.imageUrl = imageUrl;
  }

  @Override
  public void run() {
   Drawable drawable = LoadImageFromWebOperations(imageUrl);
   Message message = imageDownloadMessageHandler.obtainMessage(1, drawable);
   imageDownloadMessageHandler.sendMessage(message);
   System.out.println("Message sent");
  }

 }

 class ImageDownloadMessageHandler extends Handler {
  ProgressBar progressBar;
  View imageTextView;

  public ImageDownloadMessageHandler(ProgressBar progressBar, View imageTextView) {
   this.progressBar = progressBar;
   this.imageTextView = imageTextView;
  }

  @Override
  public void handleMessage(Message message) {
   progressBar.setVisibility(View.GONE);
   imageTextView.setBackgroundDrawable(((Drawable) message.obj));
   imageTextView.setVisibility(View.VISIBLE);
  }

 }

 Drawable LoadImageFromWebOperations(String url) {
  Drawable d = null;
  InputStream is = null;
  try {
   is = (InputStream) new URL(url).getContent();
   d = Drawable.createFromStream(is, "src name");
  } catch (MalformedURLException e) {
   e.printStackTrace();
  } catch (IOException e) {
   e.printStackTrace();
  }
  return d;
 }

}



6 comments:

  1. I tried your code and I only see progress bar. After some time, it's gone. I can't see the image !

    ReplyDelete
  2. Thanks.. it is great article.

    can you please help me out to handle multiple threads in the same above code using pool.

    ReplyDelete