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 android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
public class MainActivity extends Activity {
private ImageView imageView;
private Button button;
private final String IMAGE_PASH = "http://img.pconline.com.cn/images/upload/upc/tx/wallpaper/1312/26/c1/30020542_1388043572218.jpg";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViews();
}
private void findViews() {
imageView = (ImageView) findViewById(R.id.imageView1);
button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new ClickListener());
}
class ClickListener implements OnClickListener {
@Override
public void onClick(View arg0) {
MyTask myTask = new MyTask(MainActivity.this);
myTask.execute(IMAGE_PASH);
}
}
/**
* 异步任务执行网络下载图片
* */
// 第一个参数一般是请求的网址 第二个是执行的进度 第三个是返回的结果
public class MyTask extends AsyncTask<String, Integer, byte[]> {
Context mContext;
ProgressDialog dialog;
public MyTask(Context context) {
this.mContext = context;
}
/**
* 该方法将在执行后台耗时操作前被调用,通常该方法用于完成一些初始化的准备工作,比如界面上进度条的显示等
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
dialog = new ProgressDialog(mContext);
dialog.setTitle("提示");
dialog.setCancelable(false);
dialog.setMessage("正在下载,请等待...");
dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
dialog.show();
dialog.show();
}
/**
* 重写该方法就是后台线程将要完成的任务
* */
@Override
protected byte[] doInBackground(String... arg0) {
HttpClient httpClient = new DefaultHttpClient();
HttpGet get = new HttpGet(arg0[0]);
byte[] result = null;// 读完数据的图片的所有内容
InputStream inputStream = null;
try {
HttpResponse response = httpClient.execute(get);
long file_lenth = response.getEntity().getContentLength();// 得到所下载内容IO流的总长度
int total_lenth = 0;// 得到当前下载的进度
byte[] data = new byte[1024];
int len = 0;
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
if (response.getStatusLine().getStatusCode() == 200) {
inputStream = response.getEntity().getContent();
while ((len = inputStream.read(data)) != -1) {
total_lenth += len;
int progress_valve = (int) ((total_lenth / (float) file_lenth) * 100);// 当前下载进度的百分比
publishProgress(progress_valve);// 发布刻度单位
outputStream.write(data, 0, len);
}
}
result = outputStream.toByteArray();
} catch (Exception e) {
e.printStackTrace();
} finally {
httpClient.getConnectionManager().shutdown();// 关闭连接,释放资源
}
return result;
}
/**
* 在doInBackground()方法中调用publishProgress()方法更新任务的执行进度后,将会触发该方法
* */
@Override
protected void onProgressUpdate(Integer... values) {
super.onProgressUpdate(values);
dialog.setProgress(values[0]);
}
/**
* 当doInBackground()方法完成后,系统会自动调用onPostExecute()方法,并将doInBackground()
* 的返回值传给该方法
*
* */
@Override
protected void onPostExecute(byte[] result) {
super.onPostExecute(result);
Bitmap bitmap = BitmapFactory.decodeByteArray(result, 0,
result.length);
imageView.setImageBitmap(bitmap);
dialog.dismiss();
}
}
}
AsyncTask
点赞
收藏