DownloadManager sends STATUS_SUCCESSFUL for failed download

时光总嘲笑我的痴心妄想 提交于 2019-12-01 15:28:17

问题


Okay, I'm downloading files (images). I want to send a message with local URI for the image when the download is completed. But 20% of time I'm getting this:

6-01 18:46:39.900: INFO/DownloadManager(412): Initiating request for download 605
06-01 18:46:39.910: WARN/DownloadManager(412): Aborting request for download 605: Trying to resume a download that can't be resumed
06-01 18:46:39.910: INFO/ololo(2826): Okay, I'll broadcast.
06-01 18:46:39.990: WARN/ImageView(2826): Unable to open content: content://downloads/my_downloads/605
    java.io.FileNotFoundException: No filename found.
    at android.database.DatabaseUtils.readExceptionWithFileNotFoundExceptionFromParcel(DatabaseUtils.java:145)...
06-01 18:46:39.990: INFO/System.out(2826): resolveUri failed on bad bitmap uri: content://downloads/my_downloads/605
06-01 18:46:39.990: INFO/ololo(2826): content://downloads/my_downloads/605 was set for android.widget.ImageView@408a2cf0

Here is the code

Long downloadId = downloadIds.get(this);

if(downloadId == intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1)) {
    DownloadManager.Query query = new DownloadManager.Query();
    query.setFilterById(downloadId);
    Cursor cursor = downloadManager.query(query);

    if(cursor.moveToFirst()) {

        switch (cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_STATUS))) {
            case DownloadManager.STATUS_SUCCESSFUL : {
                Log.i("ololo", "Okay, I'll broadcast.");
                // Broadcasting
                break;
            }
            case DownloadManager.STATUS_FAILED : {
                Log.i("ololo", "Bad, I won't broadcast.");
                int reason = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_REASON));
                if(reason == DownloadManager.ERROR_CANNOT_RESUME || reason == DownloadManager.ERROR_UNKNOWN) {
                    // Rerun download
                }
                break;
            }
            default:
                break;
        }
    }
}

回答1:


DownloadManager is buggy and doesn't work correctly. This bug was filed with Google a while back:

https://code.google.com/p/android/issues/detail?id=18462

For single file download, you could workaround this by specifying a unique directory to download to specifically for this purpose, and just take whatever you get in that directory upon DownloadManager.STATUS_SUCCESSFUL.
For multi-file downloads, I can't think of a workaround at the moment, unless you have the ability to rename files at the source.



来源:https://stackoverflow.com/questions/10852821/downloadmanager-sends-status-successful-for-failed-download

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