android 文件下载及进度条显示
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;
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。