java - Android下载管理器下载位置

标签 java android download uri android-download-manager

我正在尝试使用 DownloadManager 将文件从 URL 下载到 /storage/emulated/0/Download

DownloadManager downloadManager = (DownloadManager) context.getApplicationContext().getSystemService(DOWNLOAD_SERVICE);
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
request.allowScanningByMediaScanner();
request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI | DownloadManager.Request.NETWORK_MOBILE);
request.setAllowedOverRoaming(false);
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
request.setTitle("blah");
String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getAbsoluteFile().toString();
System.out.println(path);
request.setDestinationInExternalPublicDir(path, title);
request.setVisibleInDownloadsUi(true);

final long downloadReference = downloadManager.enqueue(request);

我已经确认 path 确实返回了所需的下载路径。我的问题是,当文件实际下载时,它们最终会到达 /documents/blah ,如果卸载应用程序,文件将从设备中删除。

最佳答案

你试过吗

request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, file.name());

关于java - Android下载管理器下载位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43838374/

相关文章:

java - 决定使用 Comparable 或 Comparator

android - 在 android 2.2 中的任何地方使用 RecyclerView 刷新滚动 SwipeRefreshLayout

android - 如何在kotlin中获得许可结果

html - 如何隐藏文件链接的 URL?

java - 如何检查字符串中的空格和特殊字符(不包括下划线和句点)

java - 媒体播放器 : How to retrieve metadata with MediaMetadataRetriever?

java - Android FragmentPagerAdapter 已弃用

Android Contacts._ID != Data.CONTACT_ID

HTML下载和文本提取

php - 在我的插件中创建 WordPress 页面以将 MySQL 数据导出为 CSV