Import Java.io.ioexception;import Org.apache.http.httpentity;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.asynctask;import Android.os.bundle;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 classMainactivity extends Activity { Publicbutton button; PublicImageView ImageView; PublicProgressDialog ProgressDialog; PublicString image_path ="Http://www.eoeandroid.com/static/image/common/eoe_logo.png"; @Overrideprotected voidonCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); Button= (Button) This. Findviewbyid (R.id.button1); ImageView= (ImageView) This. Findviewbyid (R.id.imageview1); ProgressDialog=NewProgressDialog (mainactivity. This); Progressdialog.settitle ("Prompt Information"); Progressdialog.setmessage ("downloading picture, please later"); Button.setonclicklistener (NewView.onclicklistener () {@Override Public voidOnClick (View v) {//Perform an asynchronous task download picture Newmytask (). Execute (image_path); } }); } @Override Publicboolean Oncreateoptionsmenu (Menu menu) {//inflate the menu; This adds items to the action bar if it is present.getmenuinflater (). Inflate (R.menu.main, menu); return true; } //you need to inherit the Asynctask interface first, the first parameter is the execution path, the second is the progress, the third is the return value Public classMyTask extends Asynctask<string, Void, bitmap> { //time-consuming operations can be performed here protectedBitmap Doinbackground (String ...params) { //instantiating a network client objectHttpClient HttpClient =Newdefaulthttpclient (); //instantiates the Request object, and takes out the parameters in the parameter listHttpGet HttpGet =NewHttpGet (params[0]); Bitmap Bitmap=NULL; Try { //Send RequestHttpResponse HttpResponse =Httpclient.execute (HttpGet); //determine the return status and receive the picture if(Httpresponse.getstatusline (). Getstatuscode () = = $) {httpentity httpentity=httpresponse.getentity (); byte[] data =Entityutils.tobytearray (httpentity); Bitmap=bitmapfactory. Decodebytearray (Data,0, data.length); } } Catch(clientprotocolexception e) {//TODO Auto-generated catch blockE.printstacktrace (); } Catch(IOException e) {//TODO Auto-generated catch blockE.printstacktrace (); } returnbitmap; } //represents an action before a task is executed protected voidOnPreExecute () {//TODO Auto-generated method stubsSuper.onpreexecute (); //Show pop-up box before download and download picture, end popup box after downloadprogressdialog.show (); } @Override//Update UI action in this method protected voidOnPostExecute (Bitmap result) {//TODO Auto-generated method stubsSuper.onpostexecute (Result); //to set a downloaded picture for a picture controlImageview.setimagebitmap (Result); //Hide pop-up boxesProgressdialog.dismiss (); } }}
Asynctask Asynchronous Load Picture Example