Android download picture and set the progress bar

Source: Internet
Author: User

Package com.example.asynctask;

Import Java.io.ByteArrayOutputStream;
Import Java.io.InputStream;

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.support.v7.app.ActionBarActivity;
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.MenuItem;
Import Android.view.View;
Import Android.widget.Button;
Import Android.widget.ImageView;

public class Mainactivity extends Activity {

Private button button;
Private ImageView ImageView;
Private final String image_path = "Http://www.baidu.com/img/bdlogo.gif";
Private ProgressDialog Dialog;
@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
Button = (button) This.findviewbyid (R.id.button1);
ImageView = (ImageView) This.findviewbyid (R.ID.IMAGEVIEW1);
dialog = new ProgressDialog (this);
Dialog.settitle ("hint");
Dialog.setmessage ("Please wait patiently ...");
Dialog.setcancelable (FALSE);
Dialog.setprogressstyle (progressdialog.style_horizontal);
Button.setonclicklistener (New View.onclicklistener () {

@Override
public void OnClick (View v) {
TODO auto-generated Method Stub
New MyTask (). Execute (image_path);

}
});
}

public class MyTask extends Asynctask<string, Integer, byte[]>{

@Override
protected void OnPreExecute () {
TODO auto-generated Method Stub
Super.onpreexecute ();
Dialog.show ();
}
@Override
Protected byte[] Doinbackground (String ... params) {
TODO auto-generated Method Stub
HttpClient HttpClient = new Defaulthttpclient ();
HttpGet httpget = new HttpGet (params[0]);
Byte[] result =null;
InputStream inputstream = null;
Bytearrayoutputstream outputstream = new Bytearrayoutputstream ();
try{
HttpResponse HttpResponse = Httpclient.execute (HttpGet);
Long file_length = Httpresponse.getentity (). Getcontentlength ();
int total_length = 0;
byte[] data = new byte[1024];
int len = 0;
if (Httpresponse.getstatusline (). Getstatuscode () ==200) {
result = Entityutils.tobytearray (httpresponse.getentity ());
InputStream = Httpresponse.getentity (). getcontent ();
while ((Len=inputstream.read (data))!=-1) {
Total_length +=len;
int progress_value = (int) ((total_length/(float) file_length) *100);
Publishprogress (Progress_value);
Outputstream.write (Data,0,len);
}
}
result = Outputstream.tobytearray ();
}catch (Exception e) {
E.printstacktrace ();
} finally{
Httpclient.getconnectionmanager (). Shutdown ();
}
return result;
}
@Override
protected void Onprogressupdate (Integer ... values) {
TODO auto-generated Method Stub
Super.onprogressupdate (values);
Dialog.setprogress (Values[0]);
}
@Override
protected void OnPostExecute (byte[] result) {
TODO auto-generated Method Stub
Super.onpostexecute (result);
Bitmap Bitmap = Bitmapfactory.decodebytearray (result, 0, result.length);
Imageview.setimagebitmap (bitmap);
Dialog.dismiss ();
}

}
@Override
public boolean 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;
}

@Override
public boolean onoptionsitemselected (MenuItem item) {
Handle Action Bar Item clicks here. The Action Bar would
Automatically handle clicks on the Home/up button, so long
As you specify a the parent activity in Androidmanifest.xml.
int id = item.getitemid ();
if (id = = r.id.action_settings) {
return true;
}
return super.onoptionsitemselected (item);
}
}

Android download picture and set the progress bar

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.