<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]);
}
} }

最新文章

  1. java.lang.NullPointerException的可能原因及处理
  2. Jquery遮罩插件,想罩哪就罩哪!
  3. 35款加速网站开发的 CSS 开发工具
  4. 字符串-Alphabet
  5. Linux 简介
  6. BFS(染色) LA 3977 Summits
  7. SSL协议(HTTPS) 握手、工作流程详解(双向HTTPS流程)
  8. js中函数的定义
  9. HM中再增加一路自己的entropy coder
  10. Golang哲学思想
  11. 学习Slim Framework for PHP v3 (五)--route怎么被调用的?
  12. 解决Navicat Error: Missing required libmysql_d.dll
  13. “FormCRUD.csProj.FormMain.Name”隐藏了继承的成员“System.Windows.Forms.Control.Name”。如果是有意隐藏,请使用关键字 new。
  14. AngularJS测试二 jasmine测试路由 控制器 过滤器 事件 服务
  15. HDU 4614 (13年多校第二场1004)裸线段树
  16. 移动web app开发框架
  17. 15.vue使用element-ui的el-input监听不了回车事件
  18. jsoup 使用总结1--添加header
  19. 史上最全面的Docker容器引擎使用教程
  20. Jenkins使用遇到的问题总结

热门文章

  1. HDU 4521-小明序列(线段树好题)
  2. STM32学习笔记——FSMC 驱动大容量NAND FLASH [复制链接]
  3. oracle spfile和pfile文件
  4. NOIP2013 货车运输
  5. Win10系统安装
  6. 恒天云IaaS基础设施标准
  7. Android实例-拍摄和分享照片、分享文本(XE8+小米2)
  8. Judge
  9. eclipse代码左虚线对齐设置
  10. java定时任务接口ScheduledExecutorService