Getting permission denied on Android M

雨燕双飞 提交于 2019-12-24 05:53:12

问题


I'm getting PERMISSION_DENIED for WRITE_EXTERNAL_STORAGE that is in fact active:

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

In both cases I'm getting that READ_EXTERNAL_STORAGE is enabled, but WRITE_EXTERNAL_STORAGE is not!

Relevant part 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="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" />

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'

Testing on a Nexus 5X Android Version 6.0.1


回答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.



来源:https://stackoverflow.com/questions/38547480/getting-permission-denied-on-android-m

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!