问题描述
我想创建一个简单的应用程序,可以从库的应用程序得到的图像,并在一个ImageButton的显示。我用API 21上运行的Android 5.0.1手机测试。不幸的是,无论我怎么努力我不断收到安全错误 - 甚至当我指定的权限
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.
我的code获取检索图像:
My code for getting retrieving the image is:
@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;
}
}
在code工作时,我尽量选择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.
推荐答案
问题
您有误差
PROBLEM
Your have the error
java.lang.SecurityException异常:权限被拒绝:读取com.android.providers.media.MediaProvider URI内容://媒体/外部/图片/媒体/ 35634的PID = 25240,UID = 10070ṛ equires机器人.permission.READ_EXTERNAL_STORAGE 或grantUriPermission()。
原因
你是不是给正确的,因为权限的Android版本:maxSdkVersion =18
,根据文档其中。
CAUSE
You are not giving correctly permissions because of android:maxSdkVersion="18"
, which according to documentation.
在此许可应给予您的应用程序的API最高水平。如果你的应用程序需要的权限不再需要开始在某一个API级别设置该属性是非常有用的。
只要你没给的权限为 API 21
您的应用程序表现正常。
As long as you didn't give permissions to API 21
your app is behaving ok.
检查this主题进一步的信息。
解决方案:
如果你想给在读的权限 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
的权限。
ADD ON
If you want to save data you must add WRITE_EXTERNAL_STORAGE
permissions.
<uses-permission
android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
这篇关于Android的READ_EXTERNAL_STORAGE权限不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!