上篇文章写到了简单的文件下载的功能,这里再简单的添加一个功能(显示下载进度)。代码方面改变不大,只是运用了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();
}
}
}
~~~
- 前言
- 安卓ListView一个简单代码的注释和探讨
- 安卓wifi热点编程代码的若干注释
- 安卓距离传感器编程
- 简单的ScrollView
- 简单的ListView
- 简单的ArrayAdapter
- AsyncTask的初步学习
- AsyncTask再度学习
- Handler初步学习
- 开启系统Activity
- 隐式调用Activity
- Activity的生命周期
- startActivityForResult的初步学习
- 多点触控拉伸图片
- LruCache图片缓存技术
- SQLiteOpenHelper数据库操作
- 短信拦截器
- 简单的Notification
- ListView优化以及checkbox状态问题
- 安卓多线程下载
- 安卓JSON解析初步探讨
- 选项卡样式的fragment
- DrawerLayout实现简单的侧滑功能
- 安卓软引用解决图片OOM问题
- 安卓隐式Intent启动Activity和BroadcastReceiver若干注意点
- Dialog学习笔记
- ActionBar使用