我使用WebView启用了文件的下载设置。我用DownloadManager保存文件。但文件不会出现在本地下载目录中。我下载的文件保存在这里。
> file/storage/emulated/0/Android/data/com.myapp/files/x.mp3
我已经尝试了很多。但不知何故,它没有下载到本地下载文件夹中。我该怎么办?
我的守则
String string = String.valueOf((URLUtil.guessFileName(url, contentDisposition, mimeType)));
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
request.setMimeType(mimeType);
String cookies = CookieManager.getInstance().getCookie(url);
request.addRequestHeader("cookie", cookies);
request.addRequestHeader("User-Agent", userAgent);
request.setTitle("test17");
request.setDescription("Downloading file...");
request.setTitle(URLUtil.guessFileName(url, contentDisposition, mimeType));
request.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
request.setDestinationInExternalFilesDir(getContext(), DIRECTORY_DOWNLOADS , string);
DownloadManager dm = (DownloadManager)getActivity().getSystemService(DOWNLOAD_SERVICE);
dm.enqueue(request);
根据documentation,有两种类型的外部存储
- 公共文件:应该可供其他应用程序和用户免费使用的文件。当用户卸载您的应用时,这些文件应该仍然可供用户使用。例如,应用程序或其他下载文件捕获的照片应保存为公共文件。
- 私人文件:合法属于您的应用的文件,将在用户卸载您的应用时删除。虽然这些文件在技术上可由用户和其他应用程序访问,因为它们位于外部存储上,但它们不会为应用程序外的用户提供价值。
在您的代码中,调用DownloadManager.Request.setDestinationInExternalFilesDir()相当于调用Context.getExternalFilesDir()
,它将获取私有文件目录。
如果要将下载的文件保存到下载目录,请使用DownloadManager.Request.setDestinationInExternalPublicDir()
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "x.mp3");
// call allowScanningByMediaScanner() to allow media scanner to discover your file
request.allowScanningByMediaScanner();