Android Multi-threaded--handler (a) to achieve picture download

Source: Internet
Author: User

Create a child thread in the UI thread--"Get the network picture in a child thread"--"pass the binary picture through message in threads to handler,--" handler processing messages in Handlemessage ()


Package Com.example.android_handler_message;import Java.io.ioexception;import Org.apache.http.httpresponse;import Org.apache.http.client.clientprotocolexception;import Org.apache.http.client.httpclient;import Org.apache.http.client.methods.httpget;import Org.apache.http.impl.client.defaulthttpclient;import Org.apache.http.util.entityutils;import Android.os.bundle;import Android.os.handler;import android.os.Message; Import Android.app.activity;import Android.app.progressdialog;import Android.graphics.bitmap;import Android.graphics.bitmapfactory;import Android.view.menu;import Android.view.view;import Android.widget.Button;    Import Android.widget.imageview;public class Mainactivity extends Activity {private static int is_finished = 1;    Private button button = null;    Private ImageView ImageView = null;    Private String image_path = "http://i1.juyouqu.com/uploads/content//2014/04/1396611698344.jpg!w598";    Private ProgressDialog dialog = null; Private Handler handle = new Handler() {public void Handlemessage (Android.os.Message msg) {Bitmap Bitmap = null;            byte[] data = (byte[]) msg.obj;            Bitmap = Bitmapfactory.decodebytearray (data, 0, data.length);                if (msg.what = = is_finished) {Dialog.dismiss ();            Imageview.setimagebitmap (bitmap);    }        }              };        @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);                  Setcontentview (R.layout.activity_main);        Button = (button) Findviewbyid (R.id.button1);        ImageView = (ImageView) Findviewbyid (R.ID.IMAGEVIEW1);        dialog = new ProgressDialog (this);        Dialog.settitle ("hint");                            Dialog.setmessage ("Downloading ......"); Button.setonclicklistener (New View.onclicklistener () {@Override public void OnClick (V  Iew arg0) {//TODO auto-generated method stub              New Thread (New MyTask ()). Start ();            Dialog.show ();    }        }); } public class MyTask implements Runnable {@Override public void run () {//TODO Auto-gene            Rated method stub HttpClient HttpClient = new Defaulthttpclient ();            HttpGet httpget = new HttpGet (image_path);                try {HttpResponse HttpResponse = Httpclient.execute (HttpGet); if (Httpresponse.getstatusline (). Getstatuscode () = = () {byte[] data = Entityutils.toby                    Tearray (Httpresponse.getentity ());                    Message message = Message.obtain ();                    Message.obj = data;                    Message.what = is_finished;                Handle.sendmessage (message); }} catch (Clientprotocolexception e) {//TODO auto-generated catch block E.prin            Tstacktrace ();   } catch (IOException e) {             TODO auto-generated Catch block E.printstacktrace (); }        }              }}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.