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