码迷,mamicode.com
首页 > 移动开发 > 详细

android 文件下载及进度条显示

时间:2015-02-27 14:58:17      阅读:244      评论:0      收藏:0      [点我收藏+]

标签:

android 文件下载

进度条:

ProgressDialog progress = new ProgressDialog(context);

progress.setTitle(“文件下载”);

progress.setMessage("loading...");

progress.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);

progress.setIndeterminate(false);//设置为fase等待进度更新,设置为true则左右循环滚动

progress.setMax(100);

下载部分

HttpURLConnection conn = null;

URL url = null;

url = new URL(connStr);//connStr,访问路径

conn = url.openConnection();

long size = conn.getContentLength();//文件大小

BufferedInputStream in = new BufferedInputStream(conn.openStream());

ByteArrayOutputStream dataStream = new ByteArrayOutputStream();

BufferedOutputStream out = new BufferedOutputStream(dataStream);

int count = 0;

long total = 0L;

byte[] data = new byte[512];

while((count = in.read(data)) != -1){

  total += count;

  publishProgress((int)(total*100/size));//放在Asycntask里面做进度条更新;

  out.write(data,0,count);

}

out.flush();

byte[] bytes = dataStream.toByteArray();

String s = new String(bytes);//转换成对应的类型

return s;

 

android 文件下载及进度条显示

标签:

原文地址:http://www.cnblogs.com/it-wangwei/p/4303259.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!