如何将文件下载到本地下载文件夹? [英] How do i download files to the local downloads folder?

查看:107
本文介绍了如何将文件下载到本地下载文件夹?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用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

  • 公共文件:应可供其他应用和用户自由使用的文件.当用户卸载您的应用程序时,这些文件应保持对用户可用.例如,您的应用捕获的照片或其他下载的文件应另存为公共文件.
  • 私人文件:正确属于您的应用程序的文件,并且在用户卸载您的应用程序时将被删除.尽管这些文件位于外部存储上,因此从技术上讲用户和其他应用程序都可以访问这些文件,但它们并不能为应用程序外部的用户提供价值.

在您的代码中,调用

In your code, calling DownloadManager.Request.setDestinationInExternalFilesDir() is equivalent to calling Context.getExternalFilesDir() which will get private file directory.

如果要将下载的文件保存到Download目录,请使用

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();

这篇关于如何将文件下载到本地下载文件夹?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆