1. 前言
从 Android Q(10.0) sdk>=29 开始,系统为每个 app 提供了一个隔离环境(分区存储)
若想访问自己分区外的图片等,需要通过Android自带的媒体集合
参考网址:https://developer.android.google.cn/training/data-storage/files/external-scoped?hl=zh_cn
2. 解决方法:在Android Q(10.0) sdk>=29中不开启分区存储
不开启分区存储就能像Android 10之前的版本一样,获得权限就能操作存储空间中的文件
具体做法:在清单文件的application下添加这样一个属性
完整代码:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.luoking.developmenttools">
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme"
android:requestLegacyExternalStorage="true">
<activity android:name=".activity.MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>