Android asynchronously downloads pictures from the web

Source: Internet
Author: User

Package Com.example.android_asynctask;import Org.apache.http.httpentity;import org.apache.http.HttpResponse; 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 {Privatebutton button; PrivateImageView ImageView; PrivateString image_path ="http://e.hiphotos.baidu.com/image/pic/item/2fdda3cc7cd98d10b510fdea233fb80e7aec9021.jpg"; PrivateProgressDialog Dialog; @Overrideprotected voidonCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);        Setcontentview (R.layout.activity_main); Button=(Button) Findviewbyid (R.id.button1); ImageView=(ImageView) Findviewbyid (R.ID.IMAGEVIEW1); Dialog=NewProgressDialog ( This); Dialog.settitle ("Prompt Information"); Dialog.setmessage ("downloading, please wait ..."); Button.setonclicklistener (NewView.onclicklistener () {@Override Public voidOnClick (View arg0) {//TODO auto-generated Method Stub                Newmytask (). Execute (image_path);    }        }); }    /** * Rules using Asynchronous tasks: 1, declared class inheritance Asynctask callout three parameter type * 2, the first parameter represents the task to be performed, usually the path of the network, the second parameter represents the scale of the progress, and the third parameter represents the return result of the task execution * * @author Liende **/     Public classMyTask extends Asynctask<string, Void, bitmap> {        /** * Indicates action before task execution*/@Overrideprotected voidOnPreExecute () {//TODO auto-generated Method StubSuper.onpreexecute ();        Dialog.show (); }        /** * Mainly to complete time-consuming operation*/@OverrideprotectedBitmap doinbackground (String ... arg0) {//TODO auto-generated Method Stub//using the network connection class httpclient the extraction of network data from a class of Wang ChengHttpClient HttpClient =Newdefaulthttpclient (); HttpGet HttpGet=NewHttpGet (arg0[0]); Bitmap Bitmap=NULL; Try{HttpResponse HttpResponse=Httpclient.execute (HttpGet); if(Httpresponse.getstatusline (). Getstatuscode () = = $) {httpentity httpentity=httpresponse.getentity (); byte[] data =Entityutils.tobytearray (httpentity); Bitmap=bitmapfactory. Decodebytearray (Data,0, data.length); }            } Catch(Exception e) {//Todo:handle Exception            }            returnbitmap; }        /** * Mainly update the UI operation*/@Overrideprotected voidOnPostExecute (Bitmap result) {//TODO auto-generated Method StubSuper.onpostexecute (Result);            Imageview.setimagebitmap (result);        Dialog.dismiss (); }} @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; }}

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.