(1) The File Download main demo asynchronous task download picture
(2) The layout code is as follows: a imageview for storing downloaded images, button for starting asynchronous task download
<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools "android:layout_width=" match_parent "android:layout_height=" Match_parent "Android:paddi ngbottom= "@dimen/activity_vertical_margin" android:paddingleft= "@dimen/activity_horizontal_margin" Android: paddingright= "@dimen/activity_horizontal_margin" android:paddingtop= "@dimen/activity_vertical_margin" tools: Context= ". Mainactivity "> <imageview android:id=" @+id/imageview1 "android:layout_width=" Wrap_content " android:layout_height= "Wrap_content" android:layout_alignparentleft= "true" android:layout_alignparenttop= "tr UE "android:layout_marginleft=" 141DP "android:layout_margintop=" 135DP "/> <button android:id = "@+id/button1" android:layout_width= "wrap_content" android:layout_height= "Wrap_content" Android:layo Ut_alignparentbottom= "true" Android:layout_alignright= "@+id/imageview1" android:layout_marginbottom= "80DP" android:text= "Web download Picture"/></relativelayout >
(3) Control class
Package Com.example.asynctask_download;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.app.activity;import Android.app.progressdialog;import Android.graphics.bitmap;import Android.graphics.bitmapfactory;import Android.os.asynctask;import Android.os.bundle;import Android.view.menu;import Android.view.view;import Android.view.View.OnClickListener; Import Android.widget.button;import Android.widget.imageview;public class Mainactivity extends Activity {private Button Button1;private ImageView imageview1;private final String image_path = "Http://www.baidu.com/img/bd_logo1.png" ;//Set the address of the webpage picture private progressdialog progressdialog; @Overrideprotected void OnCreate (Bundle savedinstancestate) { Super.oncreate (savedinstancestate); SETCONTENTVIEW (r.layout.activity_main); button1 = (Button) This.findviewbyid (r.id.button1); imageView1 = (ImageView) This.findviewbyid (R.ID.IMAGEVIEW1);/* * Download progress bar Settings */progressdialog = new ProgressDialog (this);p Rogressdialog.settitle ("hint");p rogressdialog.setcancelable (False);p Rogressdialog.setmessage ("You are downloading pictures, please be patient!"). Button1.setonclicklistener (New Onclicklistener () {@Overridepublic void OnClick (View v) {/* * Async task, execute download picture */new MyTask (). Execute (image_path);}}); /* Async Task: Three parameters, four steps onpreexecute (), * Doinbackground (), Onprogressupdate (), OnPostExecute (), no parameters can be used when passing in the void */ public class MyTask extends Asynctask<string, Integer, byte[]> {/* * OnPreExecute () method runs on the UI thread before the background operation starts */@ overrideprotected void OnPreExecute () {super.onpreexecute ();p rogressdialog.show (); /* * Doinbackground method runs in the background and handles background operations */@Overrideprotected byte[] doinbackground (String ... params) {HttpClient HttpClient = New Defaulthttpclient (); HttpGet httpget = new HttpGet (params[0]); byte[] result = null;try {HttpResponse HttpResponse = HTTPCLIent.execute (HttpGet); if (Httpresponse.getstatusline (). Getstatuscode () = = () {result = Entityutils.tobytearray ( Httpresponse.getentity ());}} catch (Clientprotocolexception e) {e.printstacktrace ();} catch (IOException e) {e.printstacktrace ();} finally { Httpclient.getconnectionmanager (). shutdown ();} return result;} @Overrideprotected void Onprogressupdate (Integer ... values) {super.onprogressupdate (values);} /* * Once the background operation is complete, the OnPostExecute () method will run in the UI thread */@Overrideprotected void OnPostExecute (byte[] result) {Super.onpostexecute (result);/* * Place the downloaded Bitmap in Imagview * result is the downloaded picture */bitmap Bitmap = Bitmapfactory.decodebytearray (result, 0, Result.length); Imageview1.setimagebitmap (bitmap);p Rogressdialog.dismiss ();}} @Overridepublic boolean Oncreateoptionsmenu (Menu menu) {getmenuinflater (). Inflate (R.menu.main, menu); return true;}}
Results:
Android Learning Note asynctask implementation of file download tasks