问题描述
我使用WebView启用了文件的下载设置.我正在使用DownloadManager保存文件.但是这些文件不会出现在本地下载目录中.我下载的文件保存在这里.
I enabled the download settings for files with WebView. I'm saving files with DownloadManager. But the files do not appear in the local downloads directory. The files I've downloaded are save here.
> file/storage/emulated/0/Android/data/com.myapp/files/x.mp3
我已经尝试了很多.但是不知何故它没有下载到本地下载文件夹中.我该怎么办?
I've tried a lot. But somehow it was not downloaded in the local downloads folder. What should I do?
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);
推荐答案
根据文档有2种类型的外部存储空间
According to documentation there are 2 types of external storage
在您的代码中,调用 DownloadManager.Request.setDestinationInExternalFilesDir()等效于调用 Context.getExternalFilesDir()
,这将获取私有文件目录.
In your code, calling DownloadManager.Request.setDestinationInExternalFilesDir() is equivalent to calling Context.getExternalFilesDir()
which will get private file directory.
如果要将下载的文件保存到Download目录,请使用 DownloadManager.Request.setDestinationInExternalPublicDir()
If you want to save downloaded files to Download directory, use DownloadManager.Request.setDestinationInExternalPublicDir()
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "x.mp3");
// call allowScanningByMediaScanner() to allow media scanner to discover your file
request.allowScanningByMediaScanner();
这篇关于如何将文件下载到本地下载文件夹?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!