<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" > <ImageView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher"
android:maxHeight="300dp"
android:maxWidth="300dp"
android:adjustViewBounds="true"/>
<Button
android:id="@+id/btn_download"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="onDownLoadImg"
android:text="点击下载图片"
android:textSize = "20sp"/> </LinearLayout>

main.java

package com.example.day07_downloadimg_prgress;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream; import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient; 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.widget.ImageView;
import android.widget.Toast;
/**
*
//弹出进度条对话框基本使用
//创建进度条对话框
ProgressDialog progressDialog = new ProgressDialog(MainActivity.this);
//设置标题
progressDialog.setTitle("正在下载");
//设置内容
progressDialog.setMessage("下载中");
//设置弹出对话框显示的样式
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
//显示
progressDialog.show();
//给进度条设置值,在show()之后设置
progressDialog.setProgress(50);
//隐藏
progressDialog.hide();
*
*
*/
/**注意:需要在清单文件中添加网络权限
* 1.初始化控件,数据
* 2.设置下载点击事件
* 2.1.开启异步任务下载图片
* 1.先显示对话框,进度为0
* 2.开始下载
* 1.获得文件大小
* 2.获得当前下载的总量
* 3.计算当前百分比
* 4.通知对话框更新更改进度(主线程)
* 5.下载完成,返回数据
* 3.下载完成
* 1.关闭对话框
* 2.显示下载成功的图片
* @author my
*
*/
public class MainActivity extends Activity { private ImageView image;
private static final String imgPath = "http://c.hiphotos.baidu.com/image/h%3D200/sign=8cbc53a04ded2e73e3e9812cb700a16d/f7246b600c338744513e9358560fd9f9d72aa01f.jpg"; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
image = (ImageView) findViewById(R.id.image);
}
public void onDownLoadImg(View v){
new MyAsyncTask().execute(imgPath);
}
class MyAsyncTask extends AsyncTask<String, Integer, Bitmap>{
private ProgressDialog progressDialog;
@Override
protected void onPreExecute() {
super.onPreExecute();
progressDialog = new ProgressDialog(MainActivity.this);
progressDialog.setTitle("正在下载");
progressDialog.setMessage("下载中...");
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressDialog.show(); }
@Override
protected Bitmap doInBackground(String... params) {
try {
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(params[0]);
HttpResponse response = httpClient.execute(httpGet);
if(200 == response.getStatusLine().getStatusCode()){
//获得实体对象
HttpEntity entity = response.getEntity();
//得到输入流
InputStream is = entity.getContent();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
//获得要下载图片的总长度
long contentLength = entity.getContentLength();
//当前下载图片的长度
int currentLength = 0;
byte[] buf = new byte[50];
int len = 0;
while(-1 !=(len = is.read(buf))){
currentLength += len;
//计算当前的百分比
int percent =(int) ((currentLength/(float)contentLength)*100);
//通知调用主线程的方法更新进度,自动调用onProgressUpdate()方法
publishProgress(percent);
//写到流中
baos.write(buf, 0, len);
baos.flush();
}
is.close();
baos.close();
//把字节流转换成字节数组
byte[] byteArray = baos.toByteArray();
//把字节流转换成Bitmap
Bitmap bitmap = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
return bitmap;
}
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} return null;
}
@Override
protected void onPostExecute(Bitmap result) {
super.onPostExecute(result);
//关闭对话框
progressDialog.dismiss();
if(result != null){
image.setImageBitmap(result);
}else{
Toast.makeText(MainActivity.this, "网络错误", 0).show();
} }
//在主线程中执行,参数类型与类定义泛型的第二个参数一致
@Override
protected void onProgressUpdate(Integer... values) {
super.onProgressUpdate(values);
//更新进度
progressDialog.setProgress(values[0]);
}
} }

Android_AsyncTask_DownloadImg_progressDIalog的更多相关文章

随机推荐

  1. 转换Json格式帮助类

    using System; using System.Collections.Generic; using System.Text; using System.Reflection; using Sy ...

  2. NGINX(三)HASH表

    前言 nginx的hash表有几种不同的种类, 不过都是以ngx_hash_t为基础的, ngx_hash_t是最普通的hash表, 冲突采用的是链地址法, 不过这里冲突的元素不是一个链表, 而是一个 ...

  3. HDU4612 Warm up 边双(重边)缩点+树的直径

    题意:一个连通无向图,问你增加一条边后,让原图桥边最少 分析:先边双缩点,因为连通,所以消环变树,每一个树边都是桥,现在让你增加一条边,让桥变少(即形成环) 所以我们选择一条树上最长的路径,连接两端, ...

  4. HDU 1255 覆盖的面积 线段树+扫描线

    同 POJ1151 这次是两次 #include <iostream> #include <algorithm> #include <cstdio> #includ ...

  5. Zabbix探索:资产信息的妙用

    前一阵子还在考虑CMDB的问题,因此Zabbix中的Inventory,也就是所谓的资产信息,遭到了我的不少鄙视. 这几天在研究告警通知对应责任人的问题,突然想起Zabbix的资产信息中应该有这么一栏 ...

  6. bzoj 1432 [ZJOI2009]Function(找规律)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=1432 [思路] 找(cha)规(ti)律(jie) 分析戳这儿 click here ...

  7. Android 网络权限配置

    Android开发应用程序时,如果应用程序需要访问网络权限,需要在 AndroidManifest.xml 中加入以下代码 <uses-permission android:name=”andr ...

  8. mysql的group_concat的用法

    1.语法:group_concat([DISTINCT] 要连接的字段 [Order BY ASC/DESC 排序字段] [Separator '分隔符']) eg. SELECT ID, GROUP ...

  9. org.springframework.web.filter.DelegatingFilterProxy的理解

    org.springframework.web.filter.DelegatingFilterProxy可以将filter交给spring管理. 我们web.xml中配置filter时一般采用下面这种 ...

  10. HW6.1

    import java.util.Scanner; public class Solution { public static void main(String[] args) { Scanner i ...