关于AsyncTask异步下载图片带有进度条更新

首先定义一个ImageView 和一个Button:



    

    

需要添加访问网络权限。在AndroidManifest.xml中添加android.permission.INTERNET权限。

主要代码:
package com.example.http_dowloadview;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectOutputStream.PutField;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
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.ByteArrayBuffer;
import org.apache.http.util.EntityUtils;

import android.R.integer;
import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.app.ProgressDialog;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends Activity {

private Button button;
private ImageView image;
String picture = "http://static.statickksmg.com/image/2012/09/26/b120f8a66e5ab2a2c0263e02fba5cb9c.jpg";

private ProgressDialog dia;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button)findViewById(R.id.button);
image = (ImageView)findViewById(R.id.image);
//定义进度条
dia = new ProgressDialog(this);
dia.setTitle("提示信息");
dia.setMessage("正在下载、、、");
dia.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
//设置点击进度条外部,不响应;
dia.setCancelable(false);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
new MyTask().execute(picture);
}
});
}
//Sting 表示传入的值, Integer  代表进度 ,  Bitmap 代表返回的值
public class MyTask extends AsyncTask{

//任务执行之前的准备工作。
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
dia.show();
}
//主要完成耗时操作
@Override
protected Bitmap doInBackground(String... arg0) {
// TODO Auto-generated method stub
Bitmap bitmap = null;
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
InputStream inputStream = null;
try {
HttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(arg0[0]);
HttpResponse httpResponse = httpClient.execute(httpGet);
if(httpResponse.getStatusLine().getStatusCode() == 200){
inputStream = httpResponse.getEntity().getContent();
//先获得文件的总长度,
long file_length = httpResponse.getEntity().getContentLength();
int len = 0;
byte[] data = new byte[1024];
int total_length = 0;
int value = 0;
while((len = inputStream.read(data)) != -1){
total_length += len;
value = (int)((total_length/(float)file_length)*100);
//调用update函数,更新进度
publishProgress(value);
outputStream.write(data, 0, len);
}
byte[] result = outputStream.toByteArray();
;	 bitmap = BitmapFactory.decodeByteArray(result, 0, result.length);
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}finally{
if(inputStream != null){
try {
inputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return bitmap;
}
//更新进度条
@Override
protected void onProgressUpdate(Integer... values) {
// TODO Auto-generated method stub
super.onProgressUpdate(values);
dia.setProgress(values[0]);
}
//主要完成耗时操作
@Override
protected void onPostExecute(Bitmap result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
dia.dismiss();
image.setImageBitmap(result);
}
}

}

软件截图:


你可能感兴趣的:(android)