Android Development using the handler package download Picture Tool class

Source: Internet
Author: User

If every download of a picture, you have to rewrite the HTTP protocol, multi-threaded start and handler information transfer is too troublesome, we directly encapsulate a tool class, so that we can be called at any time in development.

(1) Adding permissions in the manifest file

  <uses-permission android:name= "Android.permission.INTERNET"/>

(2) Editing Tool classes

Package Com.example.g05_handler;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.annotation.suppresslint;import Android.app.ProgressDialog;import Android.content.context;import Android.os.handler;import Android.os.message;import Android.util.Log;public class  DownLoad {private ProgressDialog dialog;public DownLoad (Context context) {//TODO auto-generated Constructor Stubdialog = New ProgressDialog (context);d ialog.settitle ("hint");d ialog.setmessage ("desperate Loading");}  @SuppressLint ("Handlerleak") public void-down (final String path, final Downloadcallback callback) {final Handler Handler = New Handler () {@Overridepublic void Handlemessage (Message msg) {//TODO auto-generated method Stubsuper.handlemessage (MS g); byte[] result = (byte[]) msg.obj;Callback.download (result); if (msg.what = = 1) {Dialog.dismiss ();}}}; Class MyThread implements Runnable {@Overridepublic void Run () {//TODO auto-generated method Stubhttpclient client = new Defaulthttpclient (); HttpGet httpget = new HttpGet (path); try {HttpResponse HttpResponse = Client.execute (HttpGet); LOG.I ("TAG", "------>" + httpresponse.getstatusline (). Getstatuscode ()); if (Httpresponse.getstatusline (). Getstatuscode () = = () {byte[] result = Entityutils.tobytearray (httpresponse.getentity ()); Message message = Message.obtain (); message.obj = Result;message.what = 1;handler.sendmessage (message);}} catch (Clientprotocolexception e) {//Todo auto-generated catch Blocke.printstacktrace ();} catch (IOException e) {//TODO Auto-generated catch Blocke.printstacktrace ();} Finally {if (client! = null) {Client.getconnectionmanager (). shutdown ();}}};} New Thread (New MyThread ()). Start ();d ialog.show ();} Public interface Downloadcallback {public void download (byte[] data);}}

(3) Call the tool class

Package Com.example.g05_handler;import Com.example.g05_handler. Download.downloadcallback;import Android.os.bundle;import Android.app.activity;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 Button Butto    N    Private ImageView ImageView; Private final String path= "http://avatar.csdn.net/D/7/5/1_u013900875.jpg"; @Overrideprotected void OnCreate (Bundle Savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); button= (Button) This.findviewbyid (R.id.button1); imageview= (ImageView) This.findviewbyid (R.ID.IMAGEVIEW1); Button.setonclicklistener (New View.onclicklistener () {@Overridepublic void OnClick (View v) {//TODO auto-generated Method Stubdownload download=new downLoad (mainactivity.this);d ownload.down (Path, new Downloadcallback () {@ overridepublic void Download (byte[] DatA) {//TODO auto-generated method Stubbitmap bitmap=bitmapfactory.decodebytearray (data, 0, data.length); Imageview.setimagebitmap (bitmap);});}); @Overridepublic boolean Oncreateoptionsmenu (Menu menu) {//Inflate the menu; This adds items to the action bar if it is PR Esent.getmenuinflater (). Inflate (R.menu.main, menu); return true;}}


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.