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

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
}

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