android - Android DownloadManager 失败,原因为 470
问题描述
- 我正在使用 android.app.DownloadManager 下载片段中的文件。
- 我在开始时保存了下载 ID。我在进入片段时查询下载状态以查看文件是否正在下载。
片段中的代码是这样的:
long downloadId = viewModel.getDownloadId(); // the id downloaded last time.
DownloadManager manager = (DownloadManager) getContext().getSystemService(Context.DOWNLOAD_SERVICE);
DownloadManager.Query query = new DownloadManager.Query().setFilterById(downloadId);
Cursor c = null;
int downloadStatus = -1;
try {
c = manager.query(query);
if (c != null && c.moveToFirst()) {
downloadStatus = c.getInt(c.getColumnIndex(DownloadManager.COLUMN_STATUS));
}
} finally {
if (c != null) {
c.close();
}
}
switch (downloadStatus) {
case DownloadManager.STATUS_SUCCESSFUL:
//do something
break;
case DownloadManager.STATUS_RUNNING:
case DownloadManager.STATUS_PAUSED:
case DownloadManager.STATUS_PENDING:
// register ContentObserver to receive download progress, I query the status in `onChage` methord.
getContext().getContentResolver().registerContentObserver(
Uri.parse("content://downloads/my_downloads"), true, mDownLoadChangeObserver);
break;
default:
//show a button to download
}
//button click listener
button.setOnClickListener(v -> {
long id = manager.enqueue(request);
// save the download id
...
// register ContentObserver to receive download progress, I query the status in `onChage` methord.
getContext().getContentResolver().registerContentObserver(
Uri.parse("content://downloads/my_downloads"), true, mDownLoadChangeObserver);
});
我的问题是:
留在片段中,它工作正常。下载成功后可查询状态;
退出片段,下载仍然可以正常工作;
但是如果我在DownloadManager
下载的时候输入片段,它会去查询下载状态。查询的状态是正确的—— DownloadManager.STATUS_RUNNING
。但它立即转向DownloadManager.STATUS_FAILED
COLUMN_REASON 470。
如何解决?
解决方案
推荐阅读
- python - Remove comma only in specific locations
- html - 无法获得相同大小的输入文本框和按钮
- selenium-webdriver - 使用 Excel 为 Testng Dataprovider 获取“java.lang.NoClassDefFoundError”
- rust - 如何对 Box 进行模式匹配以获取结构的属性?
- c# - C# OpenFileDialog 为什么文件名是这样添加空格的?
- elm - 仅查询 Dict 中的前 3 个
- python - 类型对象'用户'没有属性'对象'(AbstractUser)python
- sqlbulkcopy - 更改 ConnectionString 中的 EnableBulkLoad 时 Sybase-AseBulkCopy 失败
- c# - 请求被中止 无法创建 ssl/tls 安全通道
- python - Odd sytax error in python: Invalid Syntax