Asynctask Asynchronous Load Picture Example

Source: Internet
Author: User

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

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.