1import java.io.ByteArrayOutputStream; 2import java.io.InputStream; 3import org.apache.http.HttpResponse; 4import org.apache.http.client.HttpClient; 5import org.apache.http.client.methods.HttpGet; 6import org.apache.http.impl.client.DefaultHttpClient; 7import android.app.Activity; 8import android.app.ProgressDialog; 9import android.content.Context; 10import android.graphics.Bitmap; 11import android.graphics.BitmapFactory; 12import android.os.AsyncTask; 13import android.os.Bundle; 14import android.view.View; 15import android.view.View.OnClickListener; 16import android.widget.Button; 17import android.widget.ImageView; 18public class MainActivity extends Activity { 19 private ImageView imageView; 20 private Button button; 21 private final String IMAGE_PASH = "http://img.pconline.com.cn/images/upload/upc/tx/wallpaper/1312/26/c1/30020542_1388043572218.jpg"; 22 @Override 23 protected void onCreate(Bundle savedInstanceState) { 24 super.onCreate(savedInstanceState); 25 setContentView(R.layout.activity_main); 26 findViews(); 27 } 28 private void findViews() { 29 imageView = (ImageView) findViewById(R.id.imageView1); 30 button = (Button) findViewById(R.id.button1); 31 button.setOnClickListener(new ClickListener()); 32 } 33 class ClickListener implements OnClickListener { 34 @Override 35 public void onClick(View arg0) { 36 MyTask myTask = new MyTask(MainActivity.this); 37 myTask.execute(IMAGE_PASH); 38 } 39 } 40 /** 41 * 异步任务执行网络下载图片 42 * */ 43 // 第一个参数一般是请求的网址 第二个是执行的进度 第三个是返回的结果 44 public class MyTask extends AsyncTask<String, Integer, byte[]> { 45 Context mContext; 46 ProgressDialog dialog; 47 public MyTask(Context context) { 48 this.mContext = context; 49 } 50 /** 51 * 该方法将在执行后台耗时操作前被调用,通常该方法用于完成一些初始化的准备工作,比如界面上进度条的显示等 52 * */ 53 @Override 54 protected void onPreExecute() { 55 super.onPreExecute(); 56 dialog = new ProgressDialog(mContext); 57 dialog.setTitle("提示"); 58 dialog.setCancelable(false); 59 dialog.setMessage("正在下载,请等待..."); 60 dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); 61 dialog.show(); 62 dialog.show(); 63 } 64 /** 65 * 重写该方法就是后台线程将要完成的任务 66 * */ 67 @Override 68 protected byte[] doInBackground(String... arg0) { 69 HttpClient httpClient = new DefaultHttpClient(); 70 HttpGet get = new HttpGet(arg0[0]); 71 byte[] result = null;// 读完数据的图片的所有内容 72 InputStream inputStream = null; 73 try { 74 HttpResponse response = httpClient.execute(get); 75 long file_lenth = response.getEntity().getContentLength();// 得到所下载内容IO流的总长度 76 int total_lenth = 0;// 得到当前下载的进度 77 byte[] data = new byte[1024]; 78 int len = 0; 79 ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); 80 if (response.getStatusLine().getStatusCode() == 200) { 81 inputStream = response.getEntity().getContent(); 82 while ((len = inputStream.read(data)) != -1) { 83 total_lenth += len; 84 int progress_valve = (int) ((total_lenth / (float) file_lenth) * 100);// 当前下载进度的百分比 85 publishProgress(progress_valve);// 发布刻度单位 86 outputStream.write(data, 0, len); 87 } 88 } 89 result = outputStream.toByteArray(); 90 } catch (Exception e) { 91 e.printStackTrace(); 92 } finally { 93 httpClient.getConnectionManager().shutdown();// 关闭连接,释放资源 94 } 95 return result; 96 } 97 /** 98 * 在doInBackground()方法中调用publishProgress()方法更新任务的执行进度后,将会触发该方法 99 * */ 100 @Override 101 protected void onProgressUpdate(Integer... values) { 102 super.onProgressUpdate(values); 103 dialog.setProgress(values[0]); 104 } 105 /** 106 * 当doInBackground()方法完成后,系统会自动调用onPostExecute()方法,并将doInBackground() 107 * 的返回值传给该方法 108 * 109 * */ 110 @Override 111 protected void onPostExecute(byte[] result) { 112 super.onPostExecute(result); 113 Bitmap bitmap = BitmapFactory.decodeByteArray(result, 0, 114 result.length); 115 imageView.setImageBitmap(bitmap); 116 dialog.dismiss(); 117 } 118 } 119}
AsyncTask
Stella981
2021-10-11
1087 0 0
点赞
收藏
评论区
加载中...