Android How to - Download Image With Message Passing








The following code shows how to Download Image With Message Passing.

Example

Manifest xml file

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

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="java2s.com"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.java2s.myapplication3.app.MainActivity"
            android:label="java2s.com"
            >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

    </application>

</manifest>

Main layout xml file

<?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"
    >
  <Button android:id="@+id/button"
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content"
      android:layout_gravity="center"
      android:text="Download file"
      android:onClick="startDownload" 
      />
   <TextView android:id="@+id/status"
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content"
      android:layout_gravity="center"
      android:text="click to start"
   />
</LinearLayout>

Main Activity Java code

import java.net.URL;
//  w  w  w .  j  a va2s  . c om
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends Activity implements
         Handler.Callback {

   private Handler handler = new Handler(this);

   private Runnable imageDownloader = new Runnable() {

      private void sendMessage(String what) {
         Bundle bundle = new Bundle();
         bundle.putString("status", what);
         Message message = new Message();
         message.setData(bundle);
         handler.sendMessage(message);
      }

      public void run() {
         sendMessage("Download started");
         try {
            URL imageUrl = new URL("http://www.android.com/images/froyo.png");
            Bitmap image = BitmapFactory.decodeStream(imageUrl.openStream());
            if (image != null) {
               sendMessage("Successfully retrieved file!");
            } else {
               sendMessage("Failed decoding file from stream");
            }
         } catch (Exception e) {
            sendMessage("Failed downloading file!");
            e.printStackTrace();
         }
      }
   };

   public void startDownload(View source) {
      new Thread(imageDownloader, "Download thread").start();
   }

   @Override
   public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main);
   }

   public boolean handleMessage(Message msg) {
      String text = msg.getData().getString("status");
      TextView statusText = (TextView) findViewById(R.id.status);
      statusText.setText(text);
      return true;
   }
}
null