我希望這會成爲一件我很想念的傻事,但我一直在敲着我的腦袋,試圖弄清楚我的錯在哪裏。爲什麼我的計算出的百分比在Android SDK中變爲負值?
我想從新主題中的DownloadManager更新ProgressBar。這個工作正常,直到中途,ProgressBar重置回到開始。從投入一些調試代碼,我將問題這一行:
final int dl_progress = (bytes_downloaded*100)/bytes_total;
dl_progress
通過文件下載轉陰一半!相關的代碼塊及以下日誌輸出:
@Override
public void run() {
boolean downloading = true;
while (downloading) {
DownloadManager.Query q = new DownloadManager.Query();
q.setFilterById(downloadId);
Cursor cursor = manager.query(q);
cursor.moveToFirst();
int bytes_downloaded = cursor.getInt(cursor
.getColumnIndex(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR));
int bytes_total = cursor.getInt(cursor
.getColumnIndex(DownloadManager.COLUMN_TOTAL_SIZE_BYTES));
if (cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_STATUS)) == DownloadManager.STATUS_SUCCESSFUL) {
downloading = false;
getActivity().runOnUiThread(new Runnable() {
public void run() {
mProgressBar.setVisibility(View.INVISIBLE);
}
});
}
final int dl_progress = (bytes_downloaded*100)/bytes_total;
Log.d("Download", bytes_downloaded + " of " + bytes_total + " (" + dl_progress + "%)");
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
mProgressBar.setProgress((int) dl_progress);
}
});
cursor.close();
}
}
而這裏的調試:
D/Download(18228): 7614 of 38577287 (0%)
D/Download(18228): 4226950 of 38577287 (10%)
D/Download(18228): 8578734 of 38577287 (22%)
D/Download(18228): 13207130 of 38577287 (34%)
D/Download(18228): 16539590 of 38577287 (42%)
D/Download(18228): 22287422 of 38577287 (-53%)
D/Download(18228): 28363958 of 38577287 (-37%)
D/Download(18228): 32550806 of 38577287 (-26%)
D/Download(18228): 38577287 of 38577287 (-11%)
我敢肯定這是我在做一些愚蠢的,但我不能見樹不見林在這種情況下,任何人都可以擺脫任何光線?
謝謝。時通過使用雙代替上整數100嘗試將它們相乘
溢出,溢出。 '我回來了.' – devnull
只需使用long int值 –