我使用下载管理器下载文件。为了显示下载进度,我需要文件大小。当我使用以下代码时,我将文件大小设置为-1。我如何获得正确的文件大小?
DownloadManager.Query q = new DownloadManager.Query();
q.setFilterById(downloadmanagerid1);
Cursor cursor = dm.query(q);
if (cursor.moveToFirst()) {
FileDownloading.setDownload_status(cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_STATUS)));
long fileSize = cursor.getLong(cursor.getColumnIndex(DownloadManager.COLUMN_TOTAL_SIZE_BYTES));
long bytesDL = cursor.getLong(cursor.getColumnIndex(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR));
Log.e("file size getting ",""+fileSize);
FileDownloading.setDownload_progress((int) ((bytesDL * 100.0f) / fileSize));
}
else {
FileDownloading.setDownload_progress(0);
FileDownloading.setDownload_status(-1);
}
cursor.close();
答案 0 :(得分:0)
这样做:
while ((count = input.read(data)) != -1) {
total += count;
// publishing the progress....
// After this onProgressUpdate will be called
// publishProgress("" + (int) ((total * 100) / lenghtOfFile));
// writing data to file
progressBar.setProgress((int) ((total * 100) / lenghtOfFile));
output.write(data, 0, count);
}