问题描述
我想让用户能够将文件下载到内部存储或 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 (将#修改为@)