在Android M上获得权限被拒绝

时间:2016-07-24 00:03:44

标签: android android-6.0-marshmallow android-permissions

我为WRITE_EXTERNAL_STORAGE获得PERMISSION_DENIED,实际上是活动的:

enter image description here

我正在使用一个名为Permiso的库来检索它们:

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);
}

我也尝试过使用文档中的代码:

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的相关部分:

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 And​​roid版本6.0.1上进行测试

1 个答案:

答案 0 :(得分:0)

这个问题是由于我有一个带有Manifest的模块:我在app模块清单中有权限,另一个模块也有。

将其从其他模块中删除就可以了。