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

Android下载文件的进度条提示(网络通信部分示例)

时间:2014-12-20 20:58:19      阅读:168      评论:0      收藏:0      [点我收藏+]

标签:

URL url = newURL(http://somewhere.com/some/webhosted/file);
HttpURLConnectionurlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.setDoOutput(true);
urlConnection.connect();
File SDCardRoot = Environment.getExternalStorageDirectory();
File file = new File(SDCardRoot,"somefile.ext");
FileOutputStream fileOutput = new FileOutputStream(file);
InputStream inputStream = urlConnection.getInputStream();
int totalSize = urlConnection.getContentLength();
pd.setMax(totalSize);//ProgressDialog  pd
int downloadedSize = 0;
byte[] buffer = new byte[1024];
int bufferLength = 0;
while ( (bufferLength = inputStream.read(buffer)) > 0 ) {
    fileOutput.write(buffer, 0, bufferLength);
    downloadedSize += bufferLength; ;
    pd.setProgress(downloadedSize);//ProgressDialog  pd
}

Android下载文件的进度条提示(网络通信部分示例)

标签:

原文地址:http://blog.csdn.net/berber78/article/details/42044817

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