android - 通过在具有目标 SDK 29 的 Android 10 上启用旧版外部存储,究竟选择退出范围存储
问题描述
在面向 SDK 29 时,可以选择选择范围存储。通过在清单文件中添加 android:requestLegacyExternalStorage="true" 来启用旧存储。我的应用程序的现有功能无法在 Android 10 上运行。
例如
- 无法访问公共目录 getExternalStorageDirectory()
- 无法调用 mkdir() 方法
- 无法通过 DownloadManager 等下载文件。
我想知道通过启用旧版外部存储从范围存储中选择退出的确切内容。
因为根据我的理解,我们仍然必须在 android 10 上使用新的外部存储分类。
存储路径的 DownloadManager 代码片段:
DownloadManager.Request request=new DownloadManager.Request(Download_Uri);
request.setDestinationInExternalPublicDir("/mappfolder/subdirectory", filename);
清单应用程序标签:
<application
android:requestLegacyExternalStorage="true"
android:name=".base.module.view.MyApplication"
android:allowBackup="false"
android:icon="@drawable/im_launcher"
android:label="@string/app_name"
android:largeHeap="true"
android:networkSecurityConfig="@xml/network_security_config"
android:supportsRtl="false"
android:theme="@style/myThemeNOActionBar"
tools:replace="android:supportsRtl"
>
解决方案
getExternalStorageDirectory() 在 API 29 中已弃用。使用 getExternalFilesDir()。
如果您使用旧版外部存储,您的应用需要 WRITE_EXTERNAL_STORAGE 和 READ_EXTERNAL_STORAGE 等权限。但是很快将强制实施范围存储,因此您应该更改代码以使用 MediaStore API 和/或存储访问框架。
推荐阅读
- excel - 重写 getTickCount 以使用 Private Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
- angular - 从 HTTP 响应 cookie 获取 Angular 的 cookie
- node.js - 在 Azure AKS 上无限连接到我的 tcp 服务器的套接字是什么?
- image - 解决错误或给出一个完整的工作示例以使用多图像选择器作为列表上传图像
与迪奥 - git - 如何将当前代码保存为 Git 分支并恢复早期代码?
- html - 尝试将搜索合并到响应式菜单中的问题
- java - java - 如何仅使用for循环和if在java中仅打印一次重复值
- java - 缺少后代字体字典
- sql - Select * 的行计数结果 MISMATCH 和大文件的 Hive 外部表的 Select count(1)
- python - 修复 tkinter 中小部件的位置