Java Android READ_EXTERNAL_STORAGE 权限不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32350941/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android READ_EXTERNAL_STORAGE permission not working
提问by Zheryu
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.
我正在尝试创建一个简单的应用程序,该应用程序可以从 Gallery 应用程序获取图像并将其显示在 imageButton 上。我正在运行 Android 5.0.1 的手机上使用 API 21 进行测试。不幸的是,无论我尝试什么,我都会收到安全错误 - 即使我指定了权限。
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;
}
}
The code works when I try to select an image from Dropbox, but when I select an image from gallery, I get
当我尝试从 Dropbox 选择图像时,该代码有效,但是当我从图库中选择图像时,我得到
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()
I made the use-permission tag for READ_EXTERNAL_STORAGE a child of manifest:
我将 READ_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"
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.
我在其他帖子上搜索了高低,但似乎仍然无法弄清楚为什么我仍然遇到这个讨厌的错误。
采纳答案by Jordi Castilla
PROBLEM
Your have the error
问题
你有错误
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().
java.lang.SecurityException: Permission Denial: 从 pid=25240, uid=10070 读取 com.android.providers.media.MediaProvider uri content://media/external/images/media/35634需要 android.permission.READ_EXTERNAL_STORAGE,或grantUriPermission()。
CAUSE
You are not giving correctly permissions because of android:maxSdkVersion="18"
, which according to documentation.
原因根据文档,
您没有正确授予权限,因为android:maxSdkVersion="18"
。
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 级别。如果从某个 API 级别开始不再需要您的应用所需的权限,则设置此属性很有用。
As long as you didn't give permissions to API 21
your app is behaving ok.
只要您没有授予API 21
您的应用程序权限,它就可以正常运行。
Check this topicfor further info.
查看此主题以获取更多信息。
SOLUTION
If you wanna give read permissions underAPI 21
, you must declare them as:
解决方案
如果您想在 下授予读取权限API 21
,则必须将它们声明为:
<uses-permission
android:name="android.permission.READ_EXTERNAL_STORAGE"
android:maxSdkVersion="21" />
IMPORTANT:
重要的:
This only make sense when you need to give extra permissions because some old api's require permissions that new versions doesn't, so user experience get's improved because you only ask for certain permissions when needed.
这仅在您需要授予额外权限时才有意义,因为某些旧 api 需要新版本不需要的权限,因此用户体验得到改善,因为您只在需要时请求某些权限。
So (in this case) the correct way will be:
所以(在这种情况下)正确的方法是:
<uses-permission
android:name="android.permission.READ_EXTERNAL_STORAGE" />
ADD ON
If you want to save data you must add WRITE_EXTERNAL_STORAGE
permissions.
添加
如果您想保存数据,您必须添加WRITE_EXTERNAL_STORAGE
权限。
<uses-permission
android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
回答by Rohit Jagtap
Simply replace with the below one and try,
只需用下面的替换并尝试,
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
回答by vimalp015
While debugging on some devices you need to grant permission explicitly by going to your settings --> apps --> MY_APPLICATION --> Permissions and then grant required permissions.
在某些设备上进行调试时,您需要通过转到设置 --> 应用程序 --> MY_APPLICATION --> 权限,然后授予所需权限来明确授予权限。
回答by Sridhar
In addition to adding the below in AndroidManifest.xml
除了在 AndroidManifest.xml 中添加以下内容
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
we also need to request permission in the activity for the app to actually have access to external storage. https://developer.android.com/training/permissions/requesting.html
我们还需要在 Activity 中请求应用程序实际访问外部存储的权限。https://developer.android.com/training/permissions/requesting.html