在Android M上获取权限被拒绝 [英] Getting permission denied on Android M
问题描述
我正在为WRITE_EXTERNAL_STORAGE获得PERMISSION_DENIED,而实际上它是活动的:
I'm getting PERMISSION_DENIED for WRITE_EXTERNAL_STORAGE that is in fact active:
我正在使用一个名为Permiso的库来检索它们:
I'm using a library called Permiso to retrieve them:
private void attemptStartImagePicker() {
Permiso.getInstance().requestPermissions(new Permiso.IOnPermissionResult() {
@Override
public void onPermissionResult(Permiso.ResultSet resultSet) {
if (!resultSet.isPermissionGranted(Manifest.permission.READ_EXTERNAL_STORAGE) || !resultSet.isPermissionGranted(Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
showErrorDialog(getString(R.string.change_profile_image_permission_denied));
} else {
startImagePicker();
}
}
@Override
public void onRationaleRequested(final Permiso.IOnRationaleProvided callback, String... permissions) {
Permiso.getInstance().showRationaleInDialog(getString(R.string.grant_access), getString(R.string.change_profile_image_rationale), null, new Permiso.IOnRationaleProvided() {
@Override
public void onRationaleProvided() {
callback.onRationaleProvided();
}
});
}
}, Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE);
}
我也尝试使用文档中的代码:
I've also tried with the code from the documentation:
private void requestWritePermission() {
// Here, thisActivity is the current activity
if (ContextCompat.checkSelfPermission(this,
Manifest.permission.WRITE_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
PERMISSIONS_REQUEST_WRITE_EXTERNAL);
// PERMISSIONS_REQUEST_WRITE_EXTERNAL is an
// app-defined int constant. The callback method gets the
// result of the request.
} else {
startImagePicker();
}
}
@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
switch (requestCode) {
case PERMISSIONS_REQUEST_WRITE_EXTERNAL: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// permission was granted
startImagePicker();
} else {
// permission denied
showErrorDialog(getString(R.string.change_profile_image_permission_denied));
}
return;
}
}
}
在两种情况下,我都启用了READ_EXTERNAL_STORAGE,但未启用WRITE_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="com.nothereal.packagename"
android:minSdkVersion="16"
android:targetSdkVersion="23"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
gradle的相关部分:
Relevant part of gradle:
android {
compileSdkVersion 23
buildToolsVersion '23.0.3'
defaultConfig {
minSdkVersion 16
targetSdkVersion 23
versionCode 1
versionName "1.0"
// Enabling multidex support.
multiDexEnabled true
}
}
dependencies {
compile 'com.android.support:multidex:1.0.0'
compile 'com.android.support:appcompat-v7:23.0.3'
compile 'com.android.support:design:24.1.0'
compile 'com.android.support:support-v13:24.1.0'
compile 'com.android.support:recyclerview-v7:23.0.3'
compile 'com.android.support:support-v4:23.0.3'
compile 'com.android.support:cardview-v7:23.0.3'
在Nexus 5X Android版本6.0.1上进行测试
Testing on a Nexus 5X Android Version 6.0.1
推荐答案
此问题是由于具有清单的模块不止一个:我的应用程序模块清单中有该权限,另一个模块中也有它.
This problem was due to having more than one module with a Manifest: I had the permission in my app module manifest, and another module had it too.
将其从另一个模块中删除就可以了.
Removing it from the other module did the trick.
这篇关于在Android M上获取权限被拒绝的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!