以下代码适用于Android< 6.0
下载:
DownloadManager.Request request = new DownloadManager.Request(downloadUri);
// Remove all whitespace and more char.
String fileName = pPackageBean.getSaveAsName().replaceAll("\\s+", "_").replaceAll("[^a-zA-Z0-9\\_\\-]", "");
request.setDescription(mContext.getString(R.string.application_name));
request.setTitle(pPackageBean.getSaveAsName());
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, fileName);
id = mDownloadManager.enqueue(request);
pPackageBean.setDownloadId(id);
处理成功:
case DownloadManager.STATUS_SUCCESSFUL:
long id = packageBean.getDownloadId();
Uri uri = mDownloadManager.getUriForDownloadedFile(id);
packageBean.path = uri.getEncodedPath();
因此,uri是:content://downloads/my_downloads/867
,这是不期望的。867
是下载的ID,不知道my_downloads
来自哪里。
在4.2.2上我得到:file:///storage/emulated/0/Download/Yvan_Test