DownloadManager 在 API 17 上因 ERROR_UNKNOWN 失败

DownloadManager fails with ERROR_UNKNOWN on API 17

我正在尝试使用 android 自己的 DownloadManager,它在 API 18+ 上完美运行,但相同的代码失败 (STATUS_FAILED),原因是 ERROR_UNKNOWN 几乎在我将它排入 API 17 部手机后。这是我的代码

Context context = MyApplication.getSharedContext();
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE).setTitle(notiTitle).
setVisibleInDownloadsUi(false);
File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+File.separator+fileName);
request.setDestinationUri(Uri.fromFile(file));
DownloadManager manager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
manager.enqueue(request) ;

经过几天与这个问题的斗争,我无意中发现了问题的根源。从服务器获取的下载 URL 包含“[”和“]”字符。他们在 API 18+ DownloadManager 中没有问题,但在 API 17 中下载失败并显示 ERROR_UNKNOWN,没有任何关于原因的信息。 分别用 %5B 和 %5D 替换它们解决了问题。

url = url.replace("[","%5B").replace("]","%5D");