显示下载进度

  1 package com.wangzhen.tasks;

  2 

  3 import java.io.ByteArrayOutputStream;

  4 import java.io.IOException;

  5 import java.io.InputStream;

  6 

  7 import org.apache.http.HttpEntity;

  8 import org.apache.http.HttpResponse;

  9 import org.apache.http.client.ClientProtocolException;

 10 import org.apache.http.client.methods.HttpGet;

 11 import org.apache.http.impl.client.DefaultHttpClient;

 12 

 13 import android.app.ProgressDialog;

 14 import android.content.Context;

 15 import android.graphics.Bitmap;

 16 import android.graphics.BitmapFactory;

 17 import android.os.AsyncTask;

 18 import android.widget.ImageView;

 19 

 20 /**

 21  * 下载图片

 22  * 

 23  * @author Administrator

 24  * 

 25  */

 26 public class DownloadTask extends AsyncTask<String, Integer, byte[]> {

 27 

 28     Context mContext;

 29     ImageView mImageView;

 30     ProgressDialog mDialog = null;

 31 

 32     public DownloadTask(Context context, ImageView imageView) {

 33         mContext = context;

 34         mImageView = imageView;

 35     }

 36 

 37     @Override

 38     protected void onPreExecute() {

 39         // TODO Auto-generated method stub

 40         super.onPreExecute();

 41         mDialog = new ProgressDialog(mContext);

 42         mDialog.setTitle("提示信息");

 43         mDialog.setMessage("正在下载中,请稍后");

 44         mDialog.setCancelable(false);

 45         mDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);

 46         mDialog.show();

 47     }

 48 

 49     @Override

 50     protected byte[] doInBackground(String... params) {

 51 

 52         HttpGet request = new HttpGet(params[0]);

 53         ByteArrayOutputStream BAOS = new ByteArrayOutputStream();

 54         try {

 55             HttpResponse response = new DefaultHttpClient().execute(request);

 56             if (response.getStatusLine().getStatusCode() == 200) {

 57                 HttpEntity entity = response.getEntity();

 58                 // 得到文件总长度

 59                 long file_length = entity.getContentLength();

 60                 // 每次读取后累加的总长

 61                 long total_length = 0;

 62                 byte[] buffer = new byte[1024];

 63                 InputStream inputStream = entity.getContent();

 64                 int length = 0;

 65                 while ((length = inputStream.read(buffer)) > 0) {

 66                     // 累加进度

 67                     total_length += length;

 68                     // 将数据写入ByteArrayOutputStream中

 69                     BAOS.write(buffer, 0, length);

 70                     // 计算进度

 71                     int progress = (int) (total_length * 100 / file_length);

 72                     publishProgress(progress);

 73                 }

 74             }

 75         } catch (ClientProtocolException e) {

 76             // TODO Auto-generated catch block

 77             e.printStackTrace();

 78         } catch (IOException e) {

 79             // TODO Auto-generated catch block

 80             e.printStackTrace();

 81         }

 82         return BAOS.toByteArray();

 83     }

 84 

 85     @Override

 86     protected void onPostExecute(byte[] result) {

 87         // TODO Auto-generated method stub

 88         super.onPostExecute(result);

 89         mDialog.dismiss();

 90         Bitmap bitmap = BitmapFactory.decodeByteArray(result, 0, result.length);

 91         mImageView.setImageBitmap(bitmap);

 92     }

 93 

 94     @Override

 95     protected void onProgressUpdate(Integer... values) {

 96         // TODO Auto-generated method stub

 97         super.onProgressUpdate(values);

 98         mDialog.setProgress(values[0]);

 99     }

100 }

 

你可能感兴趣的:(下载)