DownloadManager notification displays no percentage

不想你离开。 提交于 2020-02-02 15:23:28

问题


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

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

来源:https://stackoverflow.com/questions/21989922/downloadmanager-notification-displays-no-percentage

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!