Android READ_EXTERNAL_STORAGE 权限不起作用 [英] Android READ_EXTERNAL_STORAGE permission not working

查看:69
本文介绍了Android READ_EXTERNAL_STORAGE 权限不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个简单的应用程序,该应用程序可以从 Gallery 应用程序获取图像并将其显示在 imageButton 上.我正在运行 Android 5.0.1 的手机上使用 API 21 进行测试.不幸的是,无论我尝试什么,我都会收到安全错误 - 即使我指定了权限.

I'm trying to create a simple app that can get an image from the Gallery app and display it on an imageButton. I'm testing with API 21 on a phone running Android 5.0.1. Unfortunately, no matter what I try I keep getting a security error - even when I specify the permissions.

我获取图像的代码是:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent){
    super.onActivityResult(requestCode, resultCode, imageReturnedIntent);

    switch(requestCode){
        case PICK_IMAGE_REQUEST:
            if(resultCode == RESULT_OK && imageReturnedIntent != null && imageReturnedIntent.getData() != null) {
                Uri uri = imageReturnedIntent.getData();
                try {
                    Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), uri);
                    // Log.d(TAG, String.valueOf(bitmap));
                    ImageButton ib = (ImageButton) findViewById(R.id.inputImg);
                    ib.setImageBitmap(bitmap);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            break;
    }
}

当我尝试从 Dropbox 中选择图像时代码有效,但是当我从图库中选择图像时,我得到

The code works when I try to select an image from Dropbox, but when I select an image from gallery, I get

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()

我将 READ_EXTERNAL_STORAGE 的使用权限标记设为清单的子项:

I made the use-permission tag for READ_EXTERNAL_STORAGE a child of manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="[REDACTED]">

    <uses-sdk
    android:minSdkVersion="14"
    android:targetSdkVersion="23" />

    <uses-permission tools:node="replace" android:name="android.permission.READ_EXTERNAL_STORAGE" android:maxSdkVersion="18" />

    <application>
        [REDACTED]
    </application>

</manifest>

我在其他帖子上进行了高低搜索,但似乎仍然无法弄清楚为什么我仍然遇到这个讨厌的错误.

I've searched high and low on other posts, but still can't seem to figure out why I'm still getting this pesky error.

推荐答案

问题
你有错误

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" />

这篇关于Android READ_EXTERNAL_STORAGE 权限不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆