我需要一些帮助。我已经搜索了几天,试图找到解决我的问题的方法。我想在Android中下载文件时显示进度条。我已经找到足够的代码来下载文件,但是一直在努力弄清楚如何显示进度条。
这是我的下载代码:
String sURL = getString(R.string.DOWNLOAD_URL) + getString(R.string.DATABASE_FILE_NAME);
HttpClient httpclient = new DefaultHttpClient();
HttpGet httpget = new HttpGet(sURL);
HttpResponse response = httpclient.execute(httpget);
HttpEntity entity = response.getEntity();
Header[] clHeaders = response.getHeaders("Content-Length");
Header header = clHeaders[0];
int totalSize = Integer.parseInt(header.getValue());
int downloadedSize = 0;
if (entity != null) {
InputStream stream = entity.getContent();
byte buf[] = new byte[1024 * 1024];
int numBytesRead;
BufferedOutputStream fos = new BufferedOutputStream(new FileOutputStream(file));
do {
numBytesRead = stream.read(buf);
if (numBytesRead > 0) {
fos.write(buf, 0, numBytesRead);
downloadedSize += numBytesRead;
//updateProgress(downloadedSize, totalSize);
}
} while (numBytesRead > 0);
fos.flush();
fos.close();
stream.close();
httpclient.getConnectionManager().shutdown();