android - DownloadManager 发送 STATUS_SUCCESSFUL 表示下载失败

标签 android download-manager

好的,我正在下载文件(图片)。下载完成后,我想发送一 strip 有图像本地 URI 的消息。但是有 20% 的时间我得到了这个:

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

这里是代码

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;
        }
    }
}

最佳答案

DownloadManager 有问题,无法正常工作。这个错误是不久前向谷歌提交的:

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

对于单个文件下载,您可以通过指定一个专门为此目的下载的唯一目录来解决此问题,然后在 DownloadManager.STATUS_SUCCESSFUL 中获取该目录中的任何内容。
对于多文件下载,我目前想不出解决方法,除非您能够在源头重命名文件。

关于android - DownloadManager 发送 STATUS_SUCCESSFUL 表示下载失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10852821/

相关文章:

Android Download manager.COLUMN_TOTAL_SIZE_BYTES 在状态运行期间返回 -1

使用 DownloadManager 时 ListView 中的 Android ProgressBar

android - Unresolved reference : Matchers

java - Android put extra 不会将额外内容发送到另一个 Activity

java - “getOnItemSelectedListener”方法不适用于微调器

android - 运行时在Windows上编译FFmpeg错误./configure

android - 在 Android 的 DownloadManager 中设置请求正文?

android - 下载管理器android中的错误

android - 在android中控制下载管理器的下载顺序

android - 为什么没有影子出现?