如何解决Android将文件下载到SD卡
我想让用户能够将文件下载到内部存储或 SD卡(如果存在)上的共享android“下载”目录(最好)。我可以将文件下载到内部存储中,但似乎无法在SD卡上使用该文件,而我在此处查看的所有内容都已弃用,不完整或不起作用。
这是我的下载代码:
if(downloadToSdCard) {
File path = new File(browser.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS),filename);
Uri downloadsUri = FileProvider.getUriForFile(browser,"my.app.fileprovider",path);
request.setDestinationUri(downloadsUri);
} else {
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS,filename);
}
downloadManager.enqueue(request);
清单中的相关位
<application
android:requestLegacyExternalStorage="true"
...>
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="my.app.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/filepaths" />
</provider>
和filepaths.xml:
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Used when sharing images from the internet to create a temp image to download to -->
<cache-path name="shared_images" path="images/"/>
<cache-path name="text_to_speech_gen" path="text_to_speech_gen/"/>
<!-- Used when downloading files to allow external apps to open downloaded files -->
<external-path name="attachment_file" path="."/>
</paths>
我真的很想弄清楚android文件系统是如何工作的,因此希望您给出答案的解释。我曾尝试阅读官方文档,但最终却陷入了无尽的混乱兔子洞。谢谢!
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。