Android13 WRITE_EXTERNAL_STORAGE 权限失效
1. 需求及问题
- 需求是读取sdcard上txt文件
- Android13(targetSDK = 33)上取消了WRITE_EXTERNAL_STORAGE,READ_EXTERNAL_STORAGE权限。
- 取而代之的是READ_MEDIA_VIDEO,READ_MEDIA_AUDIO,READ_MEDIA_IMAGES权限
- 测试发现,即便动态申请上面三个权限,仍旧无法读取本地txt文件

2. 解决方案
- AndroidManifest.xml中增加android.permission.MANAGE_EXTERNAL_STORAGE权限
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE"/>
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.LocationDemo"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
- Activity中新增代码
Intent intent = new Intent(Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION);
intent.setData(Uri.parse("package:" + this.getPackageName()));
startActivity(intent);
![Screenshot_20230927-131444[1].png](https://p1-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/0914dc19a96e4469ae62c310b98055a2~tplv-k3u1fbpfcp-jj-mark:3024:0:0:0:q75.awebp#?w=1440&h=3040&s=161843&e=png&b=f1f0f3)
Intent intent = new Intent(Settings.ACTION_MANAGE_ALL_FILES_ACCESS_PERMISSION);
startActivity(intent);
