DownloadManager를 사용하여 활동 내에서 다운로드 진행률 표시
DownloadManager가 내 앱의 알림 표시 줄에 표시하는 것과 동일한 진행 상황을 재현하려고하는데 진행 상황이 게시되지 않습니다. runOnUiThread ()를 사용하여 업데이트하려고하는데 어떤 이유로 업데이트되지 않았습니다.
내 다운로드 :
String urlDownload = "https://dl.dropbox.com/s/ex4clsfmiu142dy/test.zip?token_hash=AAGD-XcBL8C3flflkmxjbzdr7_2W_i6CZ_3rM5zQpUCYaw&dl=1";
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(urlDownload));
request.setDescription("Testando");
request.setTitle("Download");
request.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "teste.zip");
final DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
final long downloadId = manager.enqueue(request);
final ProgressBar mProgressBar = (ProgressBar) findViewById(R.id.progressBar1);
new Thread(new Runnable() {
@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;
}
final double dl_progress = (bytes_downloaded / bytes_total) * 100;
runOnUiThread(new Runnable() {
@Override
public void run() {
mProgressBar.setProgress((int) dl_progress);
}
});
Log.d(Constants.MAIN_VIEW_ACTIVITY, statusMessage(cursor));
cursor.close();
}
}
}).start();
내 statusMessage 메서드 :
private String statusMessage(Cursor c) {
String msg = "???";
switch (c.getInt(c.getColumnIndex(DownloadManager.COLUMN_STATUS))) {
case DownloadManager.STATUS_FAILED:
msg = "Download failed!";
break;
case DownloadManager.STATUS_PAUSED:
msg = "Download paused!";
break;
case DownloadManager.STATUS_PENDING:
msg = "Download pending!";
break;
case DownloadManager.STATUS_RUNNING:
msg = "Download in progress!";
break;
case DownloadManager.STATUS_SUCCESSFUL:
msg = "Download complete!";
break;
default:
msg = "Download is nowhere in sight";
break;
}
return (msg);
}
내 로그는 완벽하게 작동하고 내 다운로드가 실행되는 동안 "다운로드 진행 중!" "다운로드 완료!"가 완료되었지만 진행 상황에서 동일하지 않습니다. 왜 그런가요? 도움이 정말 필요합니다. 다른 논리도 정말 감사합니다.
두 개의 정수를 나눕니다.
final double dl_progress = (bytes_downloaded / bytes_total) * 100;
마찬가지로 bytes_downloaded
미만이고 bytes_total
, (bytes_downloaded / bytes_total)
0이되며, 진행 그러므로 항상 0이됩니다.
계산을 다음으로 변경하십시오.
final int dl_progress = (int) ((bytes_downloaded * 100l) / bytes_total);
전체 백분위 수 (하위 임에도 불구하고)의 진행률을 얻습니다.
Paul's answer is correct but with larger downloads you will hit max int pretty quick and start getting a negative progress. I used this to solve the issue:
final int dl_progress = (int) ((bytes_downloaded * 100l) / bytes_total);
As paul said, you are dividing two integers, with result always <1.
Always cast your number before divide, which calculate and return in float-point.
Don't forget to handle DivByZero.
final int dl_progress = (int) ((double)bytes_downloaded / (double)bytes_total * 100f);
In case if someone needs implementation of download progress retriever from @Victor Laerte's question in Kotlin with RxJava here you go:
DownloadStateRetriever.kt
class DownloadStateRetriever(private val downloadManager: DownloadManager) {
fun retrieve(id: Long) {
var downloading = AtomicBoolean(true)
val disposable = Observable.fromCallable {
val query = DownloadManager.Query().setFilterById(id)
val cursor = downloadManager.query(query)
cursor.moveToFirst()
val bytesDownloaded = cursor.intValue(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR)
val bytesTotal = cursor.intValue(DownloadManager.COLUMN_TOTAL_SIZE_BYTES)
if (isSuccessful(cursor)) downloading.set(false)
cursor.close()
if (bytesTotal == 0) 0.toInt() else ((bytesDownloaded * 100F) / bytesTotal).toInt()
}
.subscribeOn(Schedulers.newThread())
.delay(1, TimeUnit.SECONDS)
.repeatUntil { !downloading.get() }
.subscribe {
Timber.i("Subscribed to $id. progress: $it")
}
}
private fun isSuccessful(cursor: Cursor) = status(cursor) == DownloadManager.STATUS_SUCCESSFUL
private fun status(cursor: Cursor) = cursor.intValue(DownloadManager.COLUMN_STATUS)
}
I have added extensions to cursor for more code clarity:
CursorExtensions.kt
import android.database.Cursor
fun Cursor.column(which: String) = this.getColumnIndex(which)
fun Cursor.intValue(which: String): Int = this.getInt(column(which))
fun Cursor.floatValue(which: String): Float = this.getFloat(column(which))
fun Cursor.stringValue(which: String): String = this.getString(column(which))
fun Cursor.doubleValue(which: String): Double = this.getDouble(column(which))
'Programing' 카테고리의 다른 글
파이썬 클래스 상속에서 독 스트링 상속 (0) | 2020.09.12 |
---|---|
IPv6 앱 스토어 거부 (0) | 2020.09.12 |
RSpec : let과 before 블록의 차이점은 무엇입니까? (0) | 2020.09.11 |
특정 태그가있는 Docker 이미지가 로컬에 있는지 확인하는 방법은 무엇입니까? (0) | 2020.09.11 |
Reactjs의 새로운 react-router-dom에서 Redirect를 사용하는 방법 (0) | 2020.09.11 |