且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Android READ_EXTERNAL_STORAGE 权限不起作用

更新时间:2022-11-16 23:21:28

问题
你有错误

java.lang.SecurityException:权限拒绝:从 pid=25240, uid=10070 读取 com.android.providers.media.MediaProvider uri content://media/external/images/media/35634 r需要 android.permission.READ_EXTERNAL_STORAGE 或 grantUriPermission().

java.lang.SecurityException: Permission Denial: reading com.android.providers.media.MediaProvider uri content://media/external/images/media/35634 from pid=25240, uid=10070 requires android.permission.READ_EXTERNAL_STORAGE, or grantUriPermission().

原因
由于 android:maxSdkVersion="18",根据文档,您没有给予正确的权限.

CAUSE
You are not giving correctly permissions because of android:maxSdkVersion="18", which according to documentation.

应向您的应用授予此权限的最高 API 级别.如果从某个 API 级别开始不再需要您的应用所需的权限,则设置此属性很有用.

The highest API level at which this permission should be granted to your app. Setting this attribute is useful if the permission your app requires is no longer needed beginning at a certain API level.

只要您没有授予 API 21 权限,您的应用就可以正常运行.

As long as you didn't give permissions to API 21 your app is behaving ok.

检查本主题了解更多信息.

解决方案
如果您想在API 21下授予读取权限,您必须将它们声明为:

SOLUTION
If you wanna give read permissions under API 21, you must declare them as:

<uses-permission 
    android:name="android.permission.READ_EXTERNAL_STORAGE"
    android:maxSdkVersion="21" />

重要提示:

这仅在您需要授予额外权限时才有意义,因为某些旧 api 需要新版本不需要的权限,因此用户体验得到改善,因为您只在需要时请求某些权限.

所以(在这种情况下)正确的方法是:

So (in this case) the correct way will be:

<uses-permission 
    android:name="android.permission.READ_EXTERNAL_STORAGE" />

添加
如果您想保存数据,您必须添加WRITE_EXTERNAL_STORAGE 权限.

<uses-permission 
     android:name="android.permission.WRITE_EXTERNAL_STORAGE" />