![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
实现AsyncTask public class MyAsyncTask extends AsyncTask<String, Integer, byte[]>
{
@Override
protected void onPreExecute()
{
super.onPreExecute();
// 在onPreExecute()中我们让ProgressDialog显示出来
progressDialog.show();
}
//返回的结果会传给onPostExecute
@Override
protected byte[] doInBackground(String... params)
{
// 通过Apache的HttpClient来访问请求网络中的一张图片
HttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(params[0]);
byte[] image = new byte[]{};
try
{
HttpResponse httpResponse = httpClient.execute(httpGet);
HttpEntity httpEntity = httpResponse.getEntity();
InputStream inputStream = null;
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
if(httpEntity != null && httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK)
{
// 得到文件的总长度
long file_length = httpEntity.getContentLength();
// 每次读取后累加的长度
long total_length = 0;
int length = 0;
// 每次读取1024个字节
byte[] data = new byte[1024];
inputStream = httpEntity.getContent();
while(-1 != (length = inputStream.read(data)))
{
// 每读一次,就将total_length累加起来
total_length += length;
// 边读边写到ByteArrayOutputStream当中
byteArrayOutputStream.write(data, 0, length);
// 得到当前图片下载的进度
int progress = (int)((total_length/(float)file_length * 100));
// 时刻将当前进度更新给onProgressUpdate方法
publishProgress(progress);
//为了看出进度条效果,让程序慢点执行
Thread.sleep(1000);
}
}
//转换为byte数组 返回给onPostExecute
image = byteArrayOutputStream.toByteArray();
inputStream.close();
byteArrayOutputStream.close();
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
httpClient.getConnectionManager().shutdown();
}
return image;
}
@Override
protected void onProgressUpdate(Integer... values)
{
super.onProgressUpdate(values);
// 更新ProgressDialog的进度条
progressDialog.setProgress(values[0]);
}
@Override
protected void onPostExecute(byte[] result)
{
super.onPostExecute(result);
// 将doInBackground方法返回的byte[]解码成要给Bitmap
Bitmap bitmap = BitmapFactory.decodeByteArray(result, 0, result.length);
// 更新我们的ImageView控件
imageView.setImageBitmap(bitmap);
// 使ProgressDialog框消失
progressDialog.dismiss();
}
}