当前位置:   article > 正文

解决Android13上读取本地文件权限错误记录

android 13 write_external_storage

Android13  WRITE_EXTERNAL_STORAGE 权限失效

1. 需求及问题

  1. 需求是读取sdcard上txt文件

  2. Android13(targetSDK = 33)上取消了WRITE_EXTERNAL_STORAGEREAD_EXTERNAL_STORAGE权限。

  3. 取而代之的是READ_MEDIA_VIDEOREAD_MEDIA_AUDIOREAD_MEDIA_IMAGES权限

  4. 测试发现,即便动态申请上面三个权限,仍旧无法读取本地txt文件

2c6403c4114ac2e21a9f61d577c36cac.png

2. 解决方案

  1. AndroidManifest.xml中增加android.permission.MANAGE_EXTERNAL_STORAGE权限

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:tools="http://schemas.android.com/tools">
  4. <uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE"/>
  5. <application
  6. android:allowBackup="true"
  7. android:dataExtractionRules="@xml/data_extraction_rules"
  8. android:fullBackupContent="@xml/backup_rules"
  9. android:icon="@mipmap/ic_launcher"
  10. android:label="@string/app_name"
  11. android:roundIcon="@mipmap/ic_launcher_round"
  12. android:supportsRtl="true"
  13. android:theme="@style/Theme.LocationDemo"
  14. tools:targetApi="31">
  15. <activity
  16. android:name=".MainActivity"
  17. android:exported="true">
  18. <intent-filter>
  19. <action android:name="android.intent.action.MAIN" />
  20. <category android:name="android.intent.category.LAUNCHER" />
  21. </intent-filter>
  22. </activity>
  23. </application>
  24. </manifest>
  1. Activity中新增代码

  1. // 方案一:跳转到系统文件访问页面,手动赋予
  2. Intent intent = new Intent(Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION);
  3. intent.setData(Uri.parse("package:" + this.getPackageName()));
  4. startActivity(intent);

cb8db8f02a2187bd28bb0939b17e8340.png

  1. // 方案二:跳转到系统所有需要文件访问页面,选择你的APP,手动赋予权限
  2. Intent intent = new Intent(Settings.ACTION_MANAGE_ALL_FILES_ACCESS_PERMISSION);
  3. startActivity(intent);

84d9bfd6b2ade68bc1559279b4af0552.png

作者:OpenGL
链接:https://juejin.cn/post/7283152332622610492

关注我获取更多知识或者投稿

990069380a14beed01e8eacfb00333f6.jpeg

ef450fdd2273b262bbb657eb43c55c46.jpeg

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号