API 26+:始终拒绝WRITE_EXTERNAL_STORAGE权限 [英] API 26+: WRITE_EXTERNAL_STORAGE permission is always denied
问题描述
我已将我的应用切换为目标API 27,但现在无法授予它WRITE_EXTERNAL_STORAGE
权限-grantResult
始终为-1
.
I have switched my app to target API 27 and now it can't be granted WRITE_EXTERNAL_STORAGE
permission -- grantResult
is always -1
.
My app needs this permission since it doesn't use apps private external storage space (which doesn't require WRITE_EXTERNAL_STORAGE
starting from API 19).
我知道API 26中存在行为更改权限.但这不能解释我的问题.
I know that in API 26 there have been behavior changes for permissions. However this doesn't explain my problem.
我正在以标准方式同时请求READ_EXTERNAL_STORAGE
和WRITE_EXTERNAL_STORAGE
权限:
I'm requesting both READ_EXTERNAL_STORAGE
and WRITE_EXTERNAL_STORAGE
permissions in a standard way:
ActivityCompat.requestPermissions(activity, new String[] {
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE
}, requestCode);
(两个权限都通过清单中的<uses-permission
声明).
(both permissions are declared via <uses-permission
in manifest).
出现对话框,然后单击允许":
The dialog appears and I click "Allow":
但是在onRequestPermissionsResult
回调中,我得到了-1
(被拒绝)用于WRITE_EXTERNAL_STORAGE
(和0
(被授予)用于READ_EXTERNAL_STORAGE
).
However inside onRequestPermissionsResult
callback I'm getting a -1
(denied) for WRITE_EXTERNAL_STORAGE
(and 0
(granted) for READ_EXTERNAL_STORAGE
).
既然我已经请求并可能同时授予了这两个结果,那么对两个结果都不会是0
吗?
Shouldn't the result be 0
for both since I have requested and, presumably, granted both?
我尝试单独请求WRITE_EXTERNAL_STORAGE
,但是在这种情况下,对话框根本不会出现.
I have tried to request WRITE_EXTERNAL_STORAGE
alone, but in this case the dialog doesn't appear at all.
更多细节:我刚刚检查了build/intermediates/manifests/full/debug
中的合并清单,并注意到WRITE_EXTERNAL_STORAGE
权限具有属性android:maxSdkVersion="18"
(清单中没有此类属性).这可能是因为我的应用程序具有minApiVersion=21
,但我不确定.
One more detail: I have just checked the merged manifest in build/intermediates/manifests/full/debug
and noticed that WRITE_EXTERNAL_STORAGE
permission has attribute android:maxSdkVersion="18"
(there is no such attribute in my manifest). This could be happening because my app has minApiVersion=21
, but I'm not sure.
推荐答案
在某个地方,您正在选择android:maxSdkVersion="18"
属性.我的猜测是它来自图书馆.在编辑自己的清单时,请检查Android Studio中的合并清单"标签.它将详细说明各种元素和属性所起的作用.
Somewhere along the line, you are picking up that android:maxSdkVersion="18"
attribute. My guess is that it is coming from a library. Check the "Merged Manifest" tab in Android Studio, when you are editing your own manifest. It will have details of what is contributing the various elements and attributes.
android:maxSdkVersion
的作用是至少在运行时权限的工作方式上,删除了更高版本的Android SDK上的<uses-permission>
元素.
android:maxSdkVersion
has the effect of removing your <uses-permission>
element on higher Android SDK versions, at least in terms of how runtime permissions work.
由于您需要所有版本的此权限,因此在<uses-permission>
元素上添加tools:remove="android:maxSdkVersion"
应该会还原android:maxSdkVersion="18"
并提供您期望的结果.
Since you need this permission for all versions, adding tools:remove="android:maxSdkVersion"
on the <uses-permission>
element should revert the android:maxSdkVersion="18"
and give you what you expect.
这篇关于API 26+:始终拒绝WRITE_EXTERNAL_STORAGE权限的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!