Android asynctask Asynchronous Download image

Source: Internet
Author: User

Package com.example.day7_practise;

Import java.io.IOException;
Import java.net.HttpURLConnection;
Import java.net.MalformedURLException;
Import Java.net.URL;
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.View;
Import Android.view.View.OnClickListener;
Import Android.widget.Button;
Import Android.widget.ImageView;
Import Android.widget.Toast;

Public class Mainactivity extends activity{
ImageView IV;
Button btn1;
@Override
protected void onCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
iv= (ImageView) Findviewbyid (R.ID.IMAGEVIEW1);
btn1= (Button) Findviewbyid (R.ID.BTN1);
Btn1.setonclicklistener (New Onclicklistener () {

@Override
public void OnClick (View v) {
Downloadtask mtask = new Downloadtask ();
String uri = "Https://ss0.bdstatic.com/5aV1bjqh_Q23odCf/static/superman/img/logo/logo_white_fe6da1ec.png";
Mtask.execute (URI);
}
});
}

Private class Downloadtask extends Asynctask<string,integer, bitmap> {
ProgressDialog pdialog;
/**
* Parameter 1 is the Doinbackground parameter set by execute in the OnClick method
* Parameter 2 for the Onprogressupdate parameter is passed in by Publishprogress (the method is used in a child thread).
* Parameter 3 is onpostexecute parameter returned by Doinbackground method
*/
//(main thread) call it before the child thread executes
@Override
protected void OnPreExecute () {
//Create dialog box
Pdialog = new ProgressDialog (mainactivity.this);
Create the title bar
pdialog.settitle ("Download Picture");
Set the progress bar style
Pdialog.setprogressstyle (progressdialog.style_spinner);
Pdialog.setmax (100);
Pdialog.setmessage ("Downloading");
Pdialog.show ();
}

(Child threads) executing child threads
@Override
Protected Bitmap doinbackground (String ... params) {
HttpURLConnection conn = null;
URL url;
try {
url = new URL (params[0]);
Conn= (HttpURLConnection) url.openconnection ();
int Code=conn.getresponsecode ();
if (code==200) {
Take out the picture information in the stream and parse it into pictures
Bitmap Bmap=bitmapfactory.decodestream (Conn.getinputstream ());
return bmap;
}else {
throw new RuntimeException ("Network access Failed");
}
} catch (Malformedurlexception e) {
E.printstacktrace ();
} catch (IOException e) {
E.printstacktrace ();
}finally {
if (conn!=null) {
Conn.disconnect ();
Conn=null;
}
}
return null;
}

(main thread) updates the UI interface when a child thread executes
@Override
protected void Onprogressupdate (Integer ... values) {
Pdialog.setprogress (value);
}

(main thread) executes after the child thread executes, wiping the buttocks dedicated
@Override
protected void OnPostExecute (Bitmap bmap) {
if (bmap==null) {
Toast.maketext (mainactivity.this, "Download Failed", 0). Show ();
}else {
Iv.setimagebitmap (BMAP);
}
Pdialog.cancel ();
}
}
}

Android asynctask Asynchronous Download image

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.