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;