我正在使用DownloadManager从Webview下载文件,并且除了MOTO G6 Play之外,几乎在所有情况下都可以使用,有人对我如何使它起作用有任何想法吗?
我的代码是:
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
request.setMimeType(mimeType);
//------------------------COOKIE!!------------------------
String cookies = CookieManager.getInstance().getCookie(url);
request.addRequestHeader("cookie", cookies);
//------------------------COOKIE!!------------------------
request.addRequestHeader("User-Agent", userAgent);
request.setDescription(getString(R.string.download_start));
request.setTitle(URLUtil.guessFileName(url, contentDisposition, mimeType));
request.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, URLUtil.guessFileName(url, contentDisposition, mimeType));
DownloadManager dm = (DownloadManager) getContext().getSystemService(DOWNLOAD_SERVICE);
dm.enqueue(request);
我试图放
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
但是没用
答案 0 :(得分:0)
尝试一下,它适用于所有版本:-
DownloadManager mgr = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
Uri downloadUri = Uri.parse(uRl);
final DownloadManager.Request request = new DownloadManager.Request(
downloadUri);
request.setAllowedNetworkTypes(
DownloadManager.Request.NETWORK_WIFI
| DownloadManager.Request.NETWORK_MOBILE)
.setVisibleInDownloadsUi(true)
.setTitle(filename)
.setAllowedOverRoaming(false)
.setTitle(filename)
.setDescription("Downloading File")
.setDestinationInExternalPublicDir("/FolderName/", filename);
request.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
assert mgr != null;
mgr.enqueue(request);