How to download Muliple files (image/video url) by DownloadManager in Android - android

Download Manager is the best way to download single file in android, It maintain Notification bar also.but how i can download Multiple files by it and show the whole downloading status by progressing bar in Notification.
Please suggest any library for it or any code snippet.

You can probably hide the DownloadManager's notification and show your own, that should do what you want.
To disable setNotificationVisibility(DownloadManger.VISIBILITY_HIDDEN); to hide the notification.
To show download progress, you can register a ContentObserver on DownloadManager's database to get periodic updates and update your own notification with it.
Cursor mDownloadManagerCursor = mDownloadManager.query(new DownloadManager.Query());
if (mDownloadManagerCursor != null) {
mDownloadManagerCursor.registerContentObserver(mDownloadFileObserver);
}
And the ContentObserver will look something like:
private ContentObserver mDownloadFileObserver = new ContentObserver(new Handler(Looper.getMainLooper())) {
#Override
public void onChange(boolean selfChange) {
Cursor cursor = mDownloadManager.query(new DownloadManager.Query());
if (cursor != null) {
long bytesDownloaded = 0;
long totalBytes = 0;
while (cursor.moveToNext()) {
bytesDownloaded += cursor.getLong(cursor.getColumnIndex(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR));
totalBytes += cursor.getLong(cursor.getColumnIndex(DownloadManager.COLUMN_TOTAL_SIZE_BYTES));
}
float progress = (float) (bytesDownloaded * 1.0 / totalBytes);
showNotificationWithProgress(progress);
cursor.close();
}
}
};
And the notification with progress can be shown with:
public void showNotificationWithProgress(Context context, int progress) {
NotificationManagerCompat.from(context).notify(0,
new NotificationCompat.Builder(context)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("Downloading...")
.setContentText("Progress")
.setProgress(100, progress * 100, false)
.setOnGoing(true)
.build());
}

Related

Notification does not show immediately even provided by a unique id

I check it many times using Logs and prevent the notify() method of manager to be called multiple times if not necessary to avoid some overhead. Now I have first notification with a 0% progress then again I create a new notification with a 0% progress again, unfortunately only one notification is shown even their id is unique but later on when the first notification progress gets updated example from 0% to 25% then that's the only time it will show the desired output, a 2 notification with different progress value. I am using only one Notification, Notification Manager, and in Notification Builder since I do not want to create an overlap animation of notification when it gets updated. Is this expected behavior when in the foreground?
public abstract class BaseTaskService extends Service {
private static final String TAG = "BaseTaskService";
private static final String CHANNEL_ID_DEFAULT = "Upload and Download";
private int queue = 0;
private FirebaseFirestore mDatabase;
private final List<Integer> listOfTaskID = new ArrayList<>();
private final SparseIntArray totalUnitList = new SparseIntArray();
private final SparseIntArray completedUnitList = new SparseIntArray();
private Notification notification;
private NotificationManager notificationManager;
private final NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, CHANNEL_ID_DEFAULT);
public void taskStarted(int id, boolean isUpload) {
//Increase the number of task
changeNumberOfTasks(1);
//Check if the task is new or not, if new then start a foreground service using id for it and add it to the list
if (!listOfTaskID.contains(id)){
listOfTaskID.add(id);
startForeground(id, notification);
Log.d(TAG, "Foreground Task Created : ID = " + id);
}
//If called by Upload Service, start the service once as a foreground per post
//If called by Download Service, start the service once as a foreground per file
if (isUpload){
//Set a total unit of files per post since one post could incorporate numerous images or files
totalUnitList.append(id, totalUnitList.get(id, 0) + 1);
Log.d(TAG, "Total Units For " + id + ": (" + totalUnitList.get(id) + ")");
}
}
public void taskCompleted() {
changeNumberOfTasks(-1);
}
private synchronized void changeNumberOfTasks(int delta) {
//Update the queue by adding delta value which could be 1 or -1
//Queue will display the overall upload or download of file from different tasks
queue += delta;
Log.d(TAG, "Overall Number of Remaining Task: " + queue);
//If there are no tasks left in queue, stop the service :)
if (queue <= 0) {
Log.d(TAG, "Stopping...");
//In Upload Service if there is no task in our queue it means that all request was finished
//so we need to reset the list of post's total task and completed task to zero
totalUnitList.clear();
completedUnitList.clear();
//Clear all of the id task
listOfTaskID.clear();
//Stop the foreground and remove all notification
stopForeground(true);
//Stop this service, calling this method will dismiss the very recent notification.
stopSelf();
}
}
#Override
public void onCreate() {
super.onCreate();
mDatabase = FirebaseFirestore.getInstance();
if (!isNotificationChannelEnabled(CHANNEL_ID_DEFAULT))
Toast.makeText(this, "Please turn on the notification in the app settings.", Toast.LENGTH_SHORT).show();
}
/*
We could use this line but unfortunately it will no longer work on Android O and above so we'll use the hashcode below.
This line is suppose to use for separating/detaching the Foreground notification from a Service
so that generating a separated unique id for PendingIntent and Finished notification is no longer needed.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
stopForeground(STOP_FOREGROUND_DETACH);
else
ServiceCompat.stopForeground(this, STOP_FOREGROUND_DETACH);
*/
//For Android O and above
private void createDefaultChannel() {
// Since Android Oreo notification channel is needed.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
//If null then initialize the Notification Manager
if (notificationManager == null)
notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
NotificationChannel channel = new NotificationChannel(CHANNEL_ID_DEFAULT,
"Upload and Download",
NotificationManager.IMPORTANCE_DEFAULT);
notificationManager.createNotificationChannel(channel);
}
}
public boolean isNotificationChannelEnabled(String channelId){
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
if(channelId != null) {
NotificationManager manager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
NotificationChannel channel = manager.getNotificationChannel(channelId);
return channel.getImportance() != NotificationManager.IMPORTANCE_NONE;
}
return false;
} else {
return NotificationManagerCompat.from(this).areNotificationsEnabled();
}
}
/**
* Show notification with a progress bar.
* Updating the progress happens here
* This is for DOWNLOAD SERVICE
*/
void showProgressNotification(String caption, long completedUnits, long totalUnits, int id) {
createDefaultChannel();
//If null then initialize the Notification Manager
if (notificationManager == null)
notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
//Compute the progress
int percentComplete = 0;
if (totalUnits > 0) {
percentComplete = (int) (100 * completedUnits / totalUnits);
}
//To update and separate the notification progress according to its task
notification = notificationBuilder
.setProgress(100, percentComplete, false)
.setContentInfo(String.valueOf(percentComplete +"%"))
.setSmallIcon(R.drawable.ic_file_upload_white_24dp)
.setContentTitle(getString(R.string.app_name))
.setContentText(caption)
.setAutoCancel(false)
.setOngoing(true)
.build();
if (!listOfTaskID.contains(id))
Log.d(TAG, "Download Notification Created: ID = " + id);
else
Log.d(TAG, "Download Notification Updated: ID = " + id);
//Notify the manager that we have a new update with notification
notificationManager.notify(id, notification);
}
/**
* Show notification with a progress bar.
* Updating the progress happens here
* This is for UPLOAD SERVICE
*/
void showProgressNotification(String caption, final String path, final int id, boolean isComplete, String title, String desc) {
createDefaultChannel();
//If null then initialize the Notification Manager
if (notificationManager == null)
notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
//Increment only if it is a successful task
if (isComplete)
completedUnitList.append(id, completedUnitList.get(id,0) + 1);
//Update and compute the progress
double percentComplete = 0;
if (totalUnitList.get(id, 0) > 0) {
//Perform this line if and only the total task is not equal to zero since dividing a number by zero is Error
percentComplete = (100 / totalUnitList.get(id)) * completedUnitList.get(id, 0);
}
notification = notificationBuilder
.setProgress(100, (int) percentComplete, false)
.setContentInfo(String.valueOf((int) percentComplete +"%"))
.setSmallIcon(R.drawable.ic_file_upload_white_24dp)
.setContentTitle(getString(R.string.app_name))
.setContentText(caption)
.setAutoCancel(false)
.setOngoing(true)
.build();
//This if condition is use to avoid repetitive call of notify() and will be triggered only if new task is created
if (!isComplete && !listOfTaskID.contains(id)){
Log.d(TAG, "Upload Notification Created: ID = " + id);
//Notify the manager that we have a new notification
notificationManager.notify(id, notification);
}
else if (isComplete){
Log.d(TAG, "Upload Notification Updated: ID = " + id);
//Notify the manager that we have a new update with notification
notificationManager.notify(id, notification);
//Check now if the number of completed task is equal to the number of total task if yes then show a finish notification
if (completedUnitList.get(id) == totalUnitList.get(id)){
Map<String, Object> details = new HashMap<>();
details.put(getResources().getString(R.string.Description), desc);
//We will use milliseconds to calculate how long is the post and for query
details.put(getResources().getString(R.string.Time_Posted), String.valueOf(new Date().getTime()));
details.put(getResources().getString(R.string.file), true);
if (title != null){
details.put(getResources().getString(R.string.Title),title);
details.put(getResources().getString(R.string.SU).toLowerCase(), Objects.requireNonNull(FirebaseAuth.getInstance().getCurrentUser()).getUid());
}
else
details.put(getResources().getString(R.string.uid), Objects.requireNonNull(FirebaseAuth.getInstance().getCurrentUser()).getUid());
//Make Intent to MainActivity
final Intent intent = new Intent(BaseTaskService.this, SUMain.class)
.putExtra(UploadService.DATA_COLLECTION, path)
.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
mDatabase.document(path).set(details).addOnSuccessListener(new OnSuccessListener<Void>() {
#Override
public void onSuccess(Void aVoid) {
showFinishedNotification(getString(R.string.upload_success), intent, true, id, true);
}
}).addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception e) {
showFinishedNotification(getString(R.string.upload_failure), intent, false, id, true);
}
});
}
}
}
/**
* Show notification that the activity finished.
*/
void showFinishedNotification(String caption, Intent intent, boolean isSuccess, int id, boolean isUpload) {
createDefaultChannel();
//Since calling a stopSelf() method will kill the service itself and dismissed the very recent Finished notification which is wrong in our case.
//Create a new id for Finished notification that is not bounded from the id of the progress notification, service, and foreground.
String uri = isUpload ? String.valueOf(intent.getParcelableExtra(UploadService.FILE_URI)) : String.valueOf(intent.getParcelableExtra(DownloadService.DOWNLOAD_URI));
//Use the hashcode of current timestamp mixed with some string to make it unique.
int newID = (uri + System.currentTimeMillis()).hashCode();
//Make PendingIntent for notification with the new generated unique id
PendingIntent pendingIntent = PendingIntent.getActivity(this, newID, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
int icon = isSuccess ? R.drawable.ic_done : R.drawable.ic_error_white_24dp;
notification = notificationBuilder
.setProgress(0, 0, false)
.setContentTitle(getString(R.string.app_name))
.setContentIntent(pendingIntent)
.setContentText(caption)
.setContentInfo(null)
.setAutoCancel(true)
.setSmallIcon(icon)
.setOngoing(false)
.build();
//Remove the first notification that has a incremental id which is the notification with progress
notificationManager.cancel(id);
//Show a new notification after removing the progress notification with the new generated unique id
notificationManager.notify(newID, notification);
Log.d(TAG, "Finished Notification: ID = " + newID);
}}

Calling setProgress(0, 0, true) does not update Notification on > API 19

Update:
After doing a lot more debugging, I figured out the cause of my notification not updating: Calling setProgress(0, 0, false) upon completion of the download. For some reason when this method is called before notify(), the upcoming update doesn't actually go through. Adding a NotificationChannel doesn't do anything.
My current workaround is to call setProgress(100, 100, false) so that the user can tell the download is finished and will subsequently get updated.
Original question:
I've got a custom file downloading utility that creates a notification whenever a file is being downloaded. This notification gets updated as the download progresses.
However, I am getting some odd results on API levels above 19. When the download is finished, the NotificationManager doesn't update the notification to inform the user of this. The notification does get updated whenever the download progresses, strangely enough.
Also, when I have the debugger active, the notification does get updated when the download completes. This leads me to believe some sort of race condition is happening here but I can't really seem to find out where or how.
My FileDownloader class:
public static void startGetRequestDownload(final Context context,
String fileUrl,
#Nullable String fileName,
#Nullable Header[] headers,
#Nullable RequestParams requestParams,
final boolean showProgressNotification){
final int notificationID = 0;
final NotificationManager mNotifyManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
if(mNotifyManager == null) {
Log.e(TAG, "Failed to get NotificationManager service");
return;
}
final NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context);
String notificationTitle = context.getString(R.string.default_filename);
if(fileName != null && !fileName.isEmpty()){
notificationTitle = fileName;
}
mBuilder.setContentTitle(notificationTitle)
.setContentText(context.getString(R.string.download_in_progress))
.setColor(ContextCompat.getColor(context, R.color.app_color_accent))
.setSmallIcon(R.drawable.ic_elab);
String uuid = "Temp_" + System.currentTimeMillis();
final File tempFile = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).toString(), uuid);
// Temporarily disable SSL as workaround.
RestClient.getClient().setSSLSocketFactory(MySSLSocketFactory.getFixedSocketFactory());
RestClient.getClient().get(context, fileUrl, headers, requestParams, new FileAsyncHttpResponseHandler(tempFile) {
#Override
public void onStart() {
super.onStart();
if(showProgressNotification){
mBuilder.setProgress(100, 0, false);
mNotifyManager.notify(notificationID, mBuilder.build());
}
}
#Override
public void onProgress(long bytesWritten, long totalSize) {
super.onProgress(bytesWritten, totalSize);
if(showProgressNotification){
int progress = (int) (bytesWritten / totalSize * 100);
mBuilder.setProgress(100, progress, false);
mNotifyManager.notify(notificationID, mBuilder.build());
}
}
#Override
public void onFailure(int statusCode, Header[] headers, Throwable throwable, File file) {
if(showProgressNotification){
mBuilder.setContentText(context.getString(R.string.download_failed));
mBuilder.setProgress(0,0,false);
mNotifyManager.notify(notificationID, mBuilder.build());
}
Log.w(TAG, "File download failed", throwable);
}
#Override
public void onSuccess(int statusCode, Header[] headers, File file) {
FileMetaData metaData = validateResponse(headers);
if(metaData != null){
final File downloadDirectory = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).toString());
File newFile = new File(downloadDirectory, metaData.fileName);
if(file.renameTo(newFile)){
Uri fileUri = FileProvider.getUriForFile(context, context.getApplicationContext().getPackageName() + ".fileprovider", newFile);
Intent actionViewIntent = new Intent(Intent.ACTION_VIEW);
actionViewIntent.setDataAndType(fileUri, metaData.contentType);
actionViewIntent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
if(showProgressNotification){
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, actionViewIntent, PendingIntent.FLAG_ONE_SHOT);
mBuilder.setProgress(0,0,false);
mBuilder.setContentText(context.getString(R.string.download_completed));
mBuilder.setContentIntent(pendingIntent);
mBuilder.setSmallIcon(android.R.drawable.stat_sys_download_done);
mNotifyManager.notify(notificationID, mBuilder.build());
}
return;
}
}
//Failover
if(showProgressNotification){
mBuilder.setContentText(context.getString(R.string.download_failed));
mNotifyManager.notify(notificationID, mBuilder.build());
}
}
});
}
I figured out the actual reason as to why my notification wasn't getting updated. The Android documentation mentions the following in regards to notification updates:
Caution: The system applies a rate limit to updating notifications. If you post updates to a notification too frequently, the system may drop some notifications.
I was calling notify() every time the download progressed. Obviously, the download progress method was called very often, which resulted in several notifications getting blocked. However, I only saw the last notification getting dropped: The download completion notification.
Limiting the amount of calls to notify() in onProgress() fixed my issue.

Android async task not stopping while download finished

I'm trying to download file using DownloadManager in Async Task. Here's the doInBackground() method
protected Boolean doInBackground(String... params) {
DownloadManager mgr = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
//String DIRECTORY_DOWNLOADS = m_context.getExternalFilesDir(null).getAbsolutePath();
Uri uri = Uri.parse(params[0]);
typ_mapy = Integer.parseInt(params[2]);
File download = new File(getExternalFilesDir(null) ,params[1]);
if(download.exists()){
info = "Existuje";
nazev_souboru=params[1];
return false;
}else{
try {
long lastDownload = mgr.enqueue(new DownloadManager.Request(uri)
.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI)
.setAllowedOverRoaming(false)
.setNotificationVisibility(Request.VISIBILITY_HIDDEN)
.setDestinationInExternalFilesDir(m_context,"/",params[1]));
Cursor c = mgr.query(new DownloadManager.Query().setFilterById(lastDownload));
while(c.moveToFirst())
{
publishProgress(String.valueOf(c.getLong(c.getColumnIndex(DownloadManager.COLUMN_TOTAL_SIZE_BYTES))), String.valueOf(c.getLong(c.getColumnIndex(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR))));
/*if(c.getLong(c.getColumnIndex(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR))>=c.getLong(c.getColumnIndex(DownloadManager.COLUMN_TOTAL_SIZE_BYTES))){
c.close();
return true;
}*/
c.close();
c = mgr.query(new DownloadManager.Query().setFilterById(lastDownload));
c.moveToFirst();
}
} catch (Exception e) {
return false;
}
return true;
}
}
You can see that I have a comment there. I thought that that condition will stop the loop, when download is finished, but when it's there, it simply calls onPostExecute() immediatly (although the download is running and finishes).
The problem is it stops calling the onProgressUpdate() where I update my progress bar.
Is there any way how to keep it in the while loop so the progress bar gets updated? If it's like this, it stays there in an endless loop. If I uncomment the condition, it finishes instantly.
EDIT:
I solved it by changing the loop like this:
boolean downloading = true;
while (downloading) {
DownloadManager.Query q = new DownloadManager.Query();
q.setFilterById(lastDownload);
Cursor cursor = mgr.query(q);
cursor.moveToFirst();
long bytes_downloaded = cursor.getLong(cursor
.getColumnIndex(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR));
long bytes_total = cursor.getLong(cursor.getColumnIndex(DownloadManager.COLUMN_TOTAL_SIZE_BYTES));
publishProgress(String.valueOf(bytes_total), String.valueOf(bytes_downloaded));
if (cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_STATUS)) == DownloadManager.STATUS_SUCCESSFUL) {
downloading = false;
}
}
Answer found here: Show Download progress inside activity using DownloadManager
Hi you have to register a broadcast receiver to receive downloading complete
you can refer https://github.com/commonsguy/cw-android/blob/master/Internet/Download/src/com/commonsware/android/download/DownloadDemo.java

Show Download Manager progress inside activity

I used Download Manager class inside my activity to perform downloads; it works fine and my next task is to show the same progress percentage inside my activity. I am not sure how to do it.
My code so far
public class DownloadSampleBook extends Activity{
private long enqueue;
private DownloadManager dm;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sample_download);
BroadcastReceiver receiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(action)) {
long downloadId = intent.getLongExtra(
DownloadManager.EXTRA_DOWNLOAD_ID, 0);
Query query = new Query();
query.setFilterById(enqueue);
Cursor c = dm.query(query);
if (c.moveToFirst()) {
int columnIndex = c
.getColumnIndex(DownloadManager.COLUMN_STATUS);
if (DownloadManager.STATUS_SUCCESSFUL == c
.getInt(columnIndex)) {
view.setImageURI(Uri.parse(uriString));
}
}
}
}
};
registerReceiver(receiver, new IntentFilter(
DownloadManager.ACTION_DOWNLOAD_COMPLETE));
}
public void onClick(View view) {
dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
Request request = new Request(
Uri.parse("http://abc.com/a.png"));
enqueue = dm.enqueue(request);
}
public void showDownload(View view) {
Intent i = new Intent();
i.setAction(DownloadManager.ACTION_VIEW_DOWNLOADS);
startActivity(i);
}
}
Is there any method that give the progress download percentage?
If you are looking for a decent way to determine when to query the DownloadManager for progress updates, consider registering a ContentObserver for the uri content://downloads/my_downloads
Example:
DownloadManager manager = (DownloadManager) getSystemService( Context.DOWNLOAD_SERVICE );
manager.enqueue( myRequest );
Uri myDownloads = Uri.parse( "content://downloads/my_downloads" );
getContentResolver().registerContentObserver( myDownloads, true, new DownloadObserver() );
...
public static class DownloadObserver extends ContentObserver {
#Override
public void onChange( boolean selfChange, Uri uri ) {
Log.d( "DownloadObserver", "Download " + uri + " updated" );
}
This yields the following output as each chunk of the long running download is received
D/DownloadObserver(15584): Download content://downloads/my_downloads/437 updated
D/DownloadObserver(15584): Download content://downloads/my_downloads/437 updated
D/DownloadObserver(15584): Download content://downloads/my_downloads/437 updated
D/DownloadObserver(15584): Download content://downloads/my_downloads/437 updated
where '437' is the ID of your download.
Note that this follows the content URI defined in the class android.provider.Downloads which appears to be hidden in the framework and may not work consistently on all devices. (https://android.googlesource.com/platform/frameworks/base/+/refs/heads/master/core/java/android/provider/Downloads.java#89)
You can query the number of bytes downloaded so far, and the total number of bytes that need to be downloaded, using the query method, in much the same way as you have queried the status in your example code. Once you have those values, it's fairly easy to calculate the progress as a percentage.
There doesn't appear to be any way for you to be notified when new data is received, so it would be up to you to poll the download manager at some regular interval to determine the current status of any download that you want to monitor.
Query query = new Query();
query.setfilterById(downloadId);
Cursor c = dm.query(query);
if (c.moveToFirst()) {
int sizeIndex = c.getColumnIndex(DownloadManager.COLUMN_TOTAL_SIZE_BYTES);
int downloadedIndex = c.getColumnIndex(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR);
long size = c.getInt(sizeIndex);
long downloaded = c.getInt(downloadedIndex);
double progress = 0.0;
if (size != -1) progress = downloaded*100.0/size;
// At this point you have the progress as a percentage.
}
Note that the total size will initially be -1 and will only be filled in once the download starts. So in the sample code above I've checked for -1 and set the progress to 0 if the size is not yet set.
However, you may find in some cases that the total size is never returned (for example, in an HTTP chunked transfer, there will be no Content-Length header from which the size can be determined). If you need to support that kind of server, you should probably provide some kind of indication to the user that the download is progressing and not just a progress bar that is stuck at zero.
I had a requirement of tracking download of multiple files. After a lot of thinking and experimenting, I came up with the following code:
private void startDownloadThread(final List<DownloadFile> list) {
// Initializing the broadcast receiver ...
mBroadCastReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
mFinishedFilesFromNotif.add(intent.getExtras()
.getLong(DownloadManager.EXTRA_DOWNLOAD_ID));
}
};
IntentFilter intentFilter = new IntentFilter(
"android.intent.action.DOWNLOAD_COMPLETE");
DownloadProgressUIFragment.this.getActivity().registerReceiver(mBroadCastReceiver,
intentFilter);
// initializing the download manager instance ....
mDownloadManager = (DownloadManager) getActivity()
.getSystemService(Context.DOWNLOAD_SERVICE);
// adding files to the download manager list ...
for(DownloadFile f: list) {
mDownloadIds.add(FileUtils.addFileForDownloadInBkg(getApplicationContext(),
f.getUrl(),
f.getPath()));
}
// starting the thread to track the progress of the download ..
mProgressThread = new Thread(new Runnable() {
#Override
public void run() {
// Preparing the query for the download manager ...
DownloadManager.Query q = new DownloadManager.Query();
long[] ids = new long[mDownloadIds.size()];
final List<Long> idsArrList= new ArrayList<>();
int i = 0;
for (Long id: mDownloadIds) {
ids[i++] = id;
idsArrList.add(id);
}
q.setFilterById(ids);
// getting the total size of the data ...
Cursor c;
while(true) {
// check if the downloads are already completed ...
// Here I have created a set of download ids from download manager to keep
// track of all the files that are dowloaded, which I populate by creating
//
if(mFinishedFilesFromNotif.containsAll(idsArrList)) {
isDownloadSuccess = true;
// TODO - Take appropriate action. Download is finished successfully
return;
}
// start iterating and noting progress ..
c = mDownloadManager.query(q);
if(c != null) {
int filesDownloaded = 0;
float fileFracs = 0f; // this stores the fraction of all the files in
// download
final int columnTotalSize = c.getColumnIndex
(DownloadManager.COLUMN_TOTAL_SIZE_BYTES);
final int columnStatus = c.getColumnIndex(DownloadManager.COLUMN_STATUS);
//final int columnId = c.getColumnIndex(DownloadManager.COLUMN_ID);
final int columnDwnldSoFar =
c.getColumnIndex(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR);
while (c.moveToNext()) {
// checking the progress ..
if(c.getInt(columnStatus) == DownloadManager.STATUS_SUCCESSFUL) {
filesDownloaded++;
}
// If the file is partially downloaded, take its fraction ..
else if(c.getInt(columnTotalSize) > 0) {
fileFracs += ((c.getInt(columnDwnldSoFar) * 1.0f) /
c.getInt(columnTotalSize));
} else if(c.getInt(columnStatus) == DownloadManager.STATUS_FAILED) {
// TODO - Take appropriate action. Error in downloading one of the
// files.
return;
}
}
c.close();
// calculate the progress to show ...
float progress = (filesDownloaded + fileFracs)/ids.length;
// setting the progress text and bar...
final int percentage = Math.round(progress * 100.0f);
final String txt = "Loading ... " + percentage + "%";
// Show the progress appropriately ...
}
}
}
});
mProgressThread.start();
}
And the function to enqueue to files are:
public static long addFileForDownloadInBkg(Context context, String url, String savePath) {
Uri uri = Uri.parse(url);
DownloadManager.Request request = new DownloadManager.Request(uri);
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_HIDDEN);
request.setDestinationUri(Uri.fromFile(new File(savePath)));
final DownloadManager m = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
return m.enqueue(request);
}
Basically, I receive a notification individually for each of the files whose download has been finished and then add them to a set which is basically the set which helps me decide if all the downloads have been finished or not. I track the the progress based on the number of files and the fraction of each being complete. I hope this helps.

DownloadManager double download

I have the following problem: Whenever I download a file with the DownloadManager it is downloaded twice (saved in the fashion "filename.extension" and "filename-1.extension"). Here is my code:
public void download() {
Request request = new Request(Uri.parse(_wrapper.getURL()));
request.setTitle(getFileName(_wrapper.getURL()));
request.setVisibleInDownloadsUi(false);
request.setDestinationInExternalFilesDir(_context, null, "/" + getFileName(_wrapper.getURL()));
_downloadID = _downloadManager.enqueue(request);
}
public BroadcastReceiver getDownloadFinishedBroadcastReceiver() {
BroadcastReceiver receiver = new BroadcastReceiver() {
#Override
public void onReceive(Context pContext, Intent pIntent) {
String action = pIntent.getAction();
if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(action)) {
Query query = new Query();
query.setFilterById(_downloadID);
Cursor cursor = _downloadManager.query(query);
if (cursor.moveToFirst()) {
File file = new File(ScruloidConstants.APPLICATION_DIRECTORY);
int status = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_STATUS));
if (status == DownloadManager.STATUS_SUCCESSFUL) {
String path = cursor.getString(cursor.getColumnIndex(DownloadManager.COLUMN_LOCAL_FILENAME));
_wrapper.setFilePath(path);
_wrapper.setLastDownloaded(new Date());
if (_listener != null) {
_listener.onDownloadProjectTaskFinished(new TaskResult<ProjectWrapper>(_wrapper));
}
}
else if (status == DownloadManager.STATUS_FAILED) {
int reason = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_REASON));
DownloadFailedException ex = new DownloadFailedException(reason);
if (_listener != null) {
_listener.onDownloadProjectTaskFinished(new TaskResult<ProjectWrapper>(ex));
}
}
}
}
}
};
return receiver;
}
The ProjectWrapper _wrapper is just a simple Class that holds data, no logic is done there. The _listener just displays on the callback method a little Toast message. I debugged my app to make shure the download() Method is invoked only once. I hope you can help me find the error.
Unfortunately, DownloadManager is buggy and doesn't work correctly on all devices. Your problem is reported here: https://code.google.com/p/android/issues/detail?id=18462
I've got the same error on mobile devices with API 21, I've made a workaround to verify before creating a request, if the file name used to set de request destination was equal one of the last files already downloaded, or if its a substring of any previews downloaded
if (!mLastMediaDownloadedId.any { it.contains(outputFile.name) }) {
mLastMediaDownloadedId.add(outputFile.name)
val url =
AppConstants.AWS_MEDIA_BUCKET_PATH + scoutObjectType.endPoint() + "$scoutObjectId.png"
val request = DownloadManager.Request(Uri.parse(url))
.setDestinationUri(Uri.fromFile(outputFile))
.setTitle("Downlading media")
.setDescription("Downloading image medias")
.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE)
.setAllowedOverRoaming(true)
.setAllowedOverMetered(true)
val downloadId = it.enqueue(request)
downloadIds.add(downloadId)
downloadId
}
and where "outputFile" is the file name itself to be downloaded, in your case this should be "filename.extension"
PS: Sorry for the Kotlin code, but it should be a good representation for the workaround itself

Categories

Resources