Show Download Progress Inside Activity Using Downloadmanager

Show Download progress inside activity using DownloadManager

You are dividing two integers:

final double dl_progress = (bytes_downloaded / bytes_total) * 100;

As bytes_downloaded is less than bytes_total, (bytes_downloaded / bytes_total) will be 0, and your progress will therefore always be 0.

Change your calculation to

final int dl_progress = (int) ((bytes_downloaded * 100l) / bytes_total);

to obtain the progress in whole (albeit floored) percentiles.

showing download progress in progress bar using DownloadManager

You have a Logic error, Your Query is out of the while loop where the UI is updated

private void startAppDownload() {

new Thread(new Runnable() {
@Override
public void run() {
boolean isDownloading = true;
int downloadStatus, totalBytesDownloaded, totalBytes;

while (isDownloading) {

DownloadManager.Query downloadQuery = new DownloadManager.Query();
downloadQuery.setFilterById(downloadID);

Cursor cursor = downloadManager.query(downloadQuery);
cursor.moveToFirst();

totalBytesDownloaded = cursor.getInt(
cursor.getColumnIndex(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR)
);

totalBytes = cursor.getInt(
cursor.getColumnIndex(DownloadManager.COLUMN_TOTAL_SIZE_BYTES)
);

downloadStatus = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_STATUS));

if(downloadStatus == DownloadManager.STATUS_SUCCESSFUL) {
isDownloading = false;
break;;
}

final int downloadProgress = (int) ((double)totalBytesDownloaded / (double)totalBytes * 100f);

runOnUiThread(new Runnable() {
@Override
public void run() {
downloadProgressBar.setProgress(downloadProgress);
}
});
}

cursor.close();
}
}).start();
}

Using ProgressBar with DownloadManager

Solution

Step 1. Declare the following variables in your class

// Indicate that we would like to update download progress
private static final int UPDATE_DOWNLOAD_PROGRESS = 1;

// Use a background thread to check the progress of downloading
private final ExecutorService executor = Executors.newFixedThreadPool(1);

// Use a hander to update progress bar on the main thread
private final Handler mainHandler = new Handler(Looper.getMainLooper(), new Handler.Callback() {
@Override
public boolean handleMessage(@NonNull Message msg) {
if (msg.what == UPDATE_DOWNLOAD_PROGRESS) {
int downloadProgress = msg.arg1;

// Update your progress bar here.
progressBar.setProgress(downloadProgress);
}
return true;
}
});

Step 2. Modify your downloadFile() method

public void downloadFile(Context context, String fileName, String fileExtension, String destinationDirectory, String url) {
DownloadManager downloadManager = (DownloadManager) context.
getSystemService(Context.DOWNLOAD_SERVICE);

Uri uri = Uri.parse(url);
DownloadManager.Request request = new DownloadManager.Request(uri);

request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
request.setDestinationInExternalFilesDir(context, destinationDirectory, fileName + fileExtension);

long downloadId = downloadManager.enqueue(request);

// Run a task in a background thread to check download progress
executor.execute(new Runnable() {
@Override
public void run() {
int progress = 0;
boolean isDownloadFinished = false;
while (!isDownloadFinished) {
Cursor cursor = downloadManager.query(new DownloadManager.Query().setFilterById(downloadId));
if (cursor.moveToFirst()) {
int downloadStatus = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_STATUS));
switch (downloadStatus) {
case DownloadManager.STATUS_RUNNING:
long totalBytes = cursor.getLong(cursor.getColumnIndex(DownloadManager.COLUMN_TOTAL_SIZE_BYTES));
if (totalBytes > 0) {
long downloadedBytes = cursor.getLong(cursor.getColumnIndex(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR));
progress = (int) (downloadedBytes * 100 / totalBytes);
}

break;
case DownloadManager.STATUS_SUCCESSFUL:
progress = 100;
isDownloadFinished = true;
break;
case DownloadManager.STATUS_PAUSED:
case DownloadManager.STATUS_PENDING:
break;
case DownloadManager.STATUS_FAILED:
isDownloadFinished = true;
break;
}
Message message = Message.obtain();
message.what = UPDATE_DOWNLOAD_PROGRESS;
message.arg1 = progress;
mainHandler.sendMessage(message);
}
}
}
});
}

Note: Remember to release the executor and handler after downloading.

executor.shutdown();
mainHandler.removeCallbacksAndMessages(null);

Progress Bar not showing with DownloadManager in android?

It doesn't work for you because your code related to tracking progress is inside a receiver which is called when the download is already finished. What you want to do is start a new thread which will poll the DownloadManager for the information in a loop until it's finished:

final long downloadId = dm.enqueue(request);

new Thread(new Runnable() {

@Override
public void run() {

while (true) {

DownloadManager.Query q = new DownloadManager.Query();
q.setFilterById(downloadId);

Cursor cursor = manager.query(q);
cursor.moveToFirst();
int bytesDownloaded = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR));
int bytesTotal = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_TOTAL_SIZE_BYTES));

if (cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_STATUS)) == DownloadManager.STATUS_SUCCESSFUL) {
break;
}

final int progress = (int) ((bytesDownloaded * 100l) / bytesTotal);

runOnUiThread(new Runnable() {

@Override
public void run() {

progressBar.setProgress(progress);

}
});
cursor.close();
}

}
}).start();

DownloadManager download progress not visible

  private fun downloadPdf(fileName: String?, fileExtension: String?, destinationDirectory: String?, url: String?) {
val downloadManager = getSystemService(Context.DOWNLOAD_SERVICE) as DownloadManager
val uri = Uri.parse(url)
val request = DownloadManager.Request(uri)
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED)
request.setDestinationInExternalPublicDir(destinationDirectory, fileName + fileExtension)
val downloadId = downloadManager.enqueue(request)

thread {
var downloading = true
while (downloading){
val query = DownloadManager.Query()
query.setFilterById(downloadId)

val cursor = downloadManager.query(query)
if(cursor.moveToFirst()){
val bytesDownloaded = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR))
val bytesTotal = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_TOTAL_SIZE_BYTES))

if(cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_STATUS)) == DownloadManager.STATUS_SUCCESSFUL){
downloading = false
}

val progress = ((bytesDownloaded * 100L)/bytesTotal).toInt()
runOnUiThread {
Log.i("pritishsawantprogress",progress.toString())
}

cursor.close()
}
}
}

}


Related Topics



Leave a reply



Submit