【Android】Async异步任务之添加进度条
来源:互联网 发布:hadoop与python 编辑:程序博客网 时间:2024/05/16 12:10
上一个blog实现了图片下载功能,但没有实现进度条,这里我们加上这个功能。
首先,将三个泛型参数的第二个改为Integer,表示进度条的刻度为int
public class MyTask extends AsyncTask<String, Integer, Bitmap>
然后,修改doInBackground方法如下:
@Overrideprotected Bitmap doInBackground(String... params) {// TODO Auto-generated method stub// 完成对图片的下载Bitmap bitmap = null;ByteArrayOutputStream outputStream = new ByteArrayOutputStream();InputStream inputStream = null;try {HttpClient httpClient = new DefaultHttpClient();HttpGet httpGet = new HttpGet(params[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;while (-1 != (len = inputStream.read(data, 0, 1024))) {total_length += len;int value = (int) ((total_length / (float) file_length) * 100);//向onPrgressUpdate传递参数publishProgress(value);outputStream.write(data, 0, len);}byte[] result = outputStream.toByteArray();bitmap = BitmapFactory.decodeByteArray(result, 0, result.length);}} catch (Exception e) {// TODO: handle exception} finally {if (inputStream != null) {try {inputStream.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}return bitmap;}
主要是修改了进度条的参数
最后在onProgressUpdate中加入:
dialog.setProgress(values[0]);
这行代码主要是处理上面的函数中publishProgress穿过来的参数。
- 【Android】Async异步任务之添加进度条
- Android之Async Task异步任务分析
- 【Android】Async异步任务
- [android]_[Async异步任务使用]
- Android有进度条异步任务下载图片
- android异步任务图片下载(进度条提示进度)
- async 异步进度条,防UI卡顿
- Android之AsyncTask异步任务
- android异步任务之IntentService
- Android带进度条的文件上传,使用AsyncTask异步任务
- android:异步任务asyncTask介绍及异步任务下载图片(带进度条)
- Http请求之android-async-http 异步框架请求
- spring boot异步(Async)任务调度
- @EnableAsync和@Async开始异步任务支持
- Spring中的@Async注解 异步任务处理
- nodejs之async异步编程
- nodejs之async异步编程
- nodejs之async异步编程
- m2eclipse插件地址,mark一下
- ViewGroup深入简出
- 在windows下使用git,并与linux服务器交互
- ExtJs中Store的几种加载方式
- 二叉树的镜像
- 【Android】Async异步任务之添加进度条
- HRBUST OJ 1908 大数加法A+B+C
- jquery的提交总结
- Object-C 类定义
- 我们需要专职的QA吗?
- 寻找两个相交链表的第一个公共节点
- Web服务器压力测试工具http_load、webbenchab、Siege使用教程
- 债券
- 10个调试和排错的小建议