1

I am trying to download files using the DownloadManager from API11+. So far file downloading has gone fine, but the action bar notification displays an undetermined size progress bar and query polling return no size until the file is completely downloaded.

Code:

DownloadManager.Request request = new DownloadManager.Request(Uri.parse(attachment.getUrl())
            .setAllowedNetworkTypes(
                    DownloadManager.Request.NETWORK_WIFI
                            | DownloadManager.Request.NETWORK_MOBILE)
            .setMimeType(attachment.getContentType())
            .setAllowedOverRoaming(true)
            .setTitle(attachment.getName())
            .setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS,
                    attachment.getName())
            .setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED)
            .addRequestHeader("Content-Length", attachment.getSizeInBytes() + "");
long lastDownload = mDownloadManager.enqueue(request);

and my query is

  double progress = 0.0;
  query.setFilterById(downloadId);
  Cursor cursor = mDownloadManager.query(query);
  if (cursor.moveToFirst()) {
      int sizeIndex = cursor.getColumnIndex(DownloadManager.COLUMN_TOTAL_SIZE_BYTES);
      int downloadedIndex = cursor
              .getColumnIndex(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR);
      long size = cursor.getInt(sizeIndex);
      long downloaded = cursor.getInt(downloadedIndex);
      Log.e("THINGS", "Downloaded " + downloaded + " of a total of " + size);
      if (size != -1) {
          progress = downloaded * 100.0 / size;
      }
  }

The log statement in that block returns

        // Downloaded 4127 of a total of -1
        // Downloaded 842903 of a total of -1
        // Downloaded 1440759 of a total of 1440759

and the notification is similar to this

Undetermined size

Can anyone point out what am I doing wrong, or if the problem resides on the server?

MLProgrammer-CiM
  • 17,231
  • 5
  • 42
  • 75
  • My guess is that the server isn't telling the download manager the size of the content it's going to send – Zharf Feb 24 '14 at 14:04
  • See this, it says something about having your own File Observer for this - http://stackoverflow.com/questions/7824835/android-downloadmanager-progress – Atul O Holic Feb 24 '14 at 14:10
  • i couldn't display this notification bar. How you displayed this? here is query. https://stackoverflow.com/questions/45457963/on-progress-seekbar-notification-not-showing-in-downloadmanager-android – Shadow Aug 03 '17 at 06:10

0 Answers0