Android AsyncTask异步实现大文件下载
![异步下载显示图片](http:
public class Third extends Activity {
private Button btn;
private ImageView iv;
private TextView textview;
private Handler handlers;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.download);
btn = (Button) findViewById(R.id.btn);
iv = (ImageView) findViewById(R.id.iv);
textview = (TextView) findViewById(R.id.textview);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
DownLoadImageTask imt = new DownLoadImageTask();
imt.execute("http://img2.niutuku.com/desk/1208/0850/bizhi-0850-17120.jpg");
}
});
handlers = new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
if(msg.what==0x001){
Log.e("---------------------", "" + msg.obj);
textview.setText(msg.obj.toString());
}else if(msg.what==0x002){
Bitmap bitmap = (Bitmap) msg.obj;
iv.setImageBitmap(bitmap);
}
}
};
}
class DownLoadImageTask
extends AsyncTask<String, Integer, byte[]> {
private ProgressDialog progressDialog;
private byte[] current = new byte[2 * 1024];
private byte[] total;
private boolean flag;
@Override
protected void onPreExecute() {
progressDialog = new ProgressDialog(Third.this);
progressDialog.setMessage("正在操作中,请稍候……");
progressDialog.setMax(100);
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressDialog.setIndeterminate(false);
flag = true;
progressDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
flag = false;
}
});
progressDialog.show();
}
@Override
protected byte[] doInBackground(String... params) {
try {
URL url = new URL(params[0]);
HttpURLConnection http = (HttpURLConnection) url.openConnection();
http.setConnectTimeout(5000);
int length = http.getContentLength();
Log.d("=======", length + "");
total = new byte[length];
int pointer = 0;
InputStream is = http.getInputStream();
int real = is.read(current);
while(flag && real > 0){
for(int i = 0; i < real; i ++){
total[pointer + i] = current[i];
}
pointer += real;
int progress = (int)((double)pointer / length * 100);
publishProgress(progress, pointer, length);
real = is.read(current);
}
is.close();
return total;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onProgressUpdate(Integer... values) {
progressDialog.setMessage(
String.format("已读%d M, 共%d M",
values[1] / 1024 / 1024, values[2] / 1024 / 1024));
progressDialog.setProgress(values[0]);
Message message001 = handlers.obtainMessage(0x001);
message001.obj=values[0];
handlers.sendMessage(message001);
}
@Override
protected void onCancelled() {
super.onCancelled();
Toast.makeText(getApplication(),"任务已取消",Toast.LENGTH_SHORT).show();
}
@Override
protected void onPostExecute(byte[] result) {
progressDialog.dismiss();
Bitmap bitmap = null;
if(flag){
bitmap = BitmapFactory.decodeByteArray(
result, 0, result.length);
Message message002 = handlers.obtainMessage(0x002);
message002.obj=bitmap;
handlers.sendMessage(message002);
}
flag = false;
if(!flag && !bitmap.isRecycled()){
}
}
}
}
download.xml文件:-----------------------------------
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:id="@+id/iv"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<ProgressBar
style="?android:attr/progressBarStyleHorizontal"
android:layout_gravity="center_vertical|center_horizontal"
android:id="@+id/progressbar"
android:layout_width="200dp"
android:layout_height="20dp" />
<TextView
android:textColor="#FF0000"
android:id="@+id/textview"
android:layout_below="@id/progressbar"
android:gravity="center_horizontal"
android:textSize="14dp"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="@android:color/holo_blue_dark"
android:text="下载图片"
android:textColor="#FFFFFF" />
</RelativeLayout>