AsyncTask再度学习
最后更新于:2022-04-01 14:43:10
上篇文章写到了简单的文件下载的功能,这里再简单的添加一个功能(显示下载进度)。代码方面改变不大,只是运用了AsyncTask的onProgressUpdate函数,不过下载图片的代码得要用另外一种方式写了。
~~~
package com.example.myasynctask;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import org.apache.http.HttpEntity;
import org.apache.http.HttpRequest;
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.EntityUtils;
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 iv;
private String path = "http://cdn.duitang.com/uploads/item/201310/17/20131017194234_eZfFU.thumb.600_0.png";
private ProgressDialog dia;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) this.findViewById(R.id.bt);
iv = (ImageView) this.findViewById(R.id.iv);
dia = new ProgressDialog(this);
dia.setTitle("提示信息");
dia.setMessage("正在下载...");
dia.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
new myTask().execute(path);
}
});
}
private class myTask extends AsyncTask<String, Integer, Bitmap> {
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
dia.show();
}
@Override
protected void onProgressUpdate(Integer... values) {
// TODO Auto-generated method stub
super.onProgressUpdate(values);
dia.setProgress(values[0]);
}
@Override
protected Bitmap doInBackground(String... params) {
// TODO Auto-generated method stub
Bitmap bitmap = null;
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
InputStream inputStream = null;
HttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(params[0]);
try {
HttpResponse httpResponse = httpClient.execute(httpGet);
if (httpResponse.getStatusLine().getStatusCode() == 200) {
long total_length = httpResponse.getEntity()
.getContentLength();
int has_read = 0;
int total_read = 0;
byte[] data = new byte[1024];
inputStream = httpResponse.getEntity().getContent();
while ((has_read = inputStream.read(data)) != -1) {
total_read += has_read;
outputStream.write(data, 0, has_read);
int value = (int) (total_read / (float) total_length * 100);
publishProgress(value);
}
byte[] result = outputStream.toByteArray();
bitmap = BitmapFactory.decodeByteArray(result, 0,
result.length);
}
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return bitmap;
}
@Override
protected void onPostExecute(Bitmap result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
iv.setImageBitmap(result);
dia.dismiss();
}
}
}
~~~