首页 > 解决方案 > 通过在具有目标 SDK 29 的 Android 10 上启用旧版外部存储,究竟选择退出范围存储

问题描述

在面向 SDK 29 时,可以选择选择范围存储。通过在清单文件中添加 android:requestLegacyExternalStorage="true" 来启用旧存储。我的应用程序的现有功能无法在 Android 10 上运行。

例如

  1. 无法访问公共目录 getExternalStorageDirectory()
  2. 无法调用 mkdir() 方法
  3. 无法通过 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"
        >

标签: androidscoped-storage

解决方案


getExternalStorageDirectory() 在 API 29 中已弃用。使用 getExternalFilesDir()。

如果您使用旧版外部存储,您的应用需要 WRITE_EXTERNAL_STORAGE 和 READ_EXTERNAL_STORAGE 等权限。但是很快将强制实施范围存储,因此您应该更改代码以使用 MediaStore API 和/或存储访问框架。


推荐阅读