Lpuche
Lpuche

Reputation: 139

Android Studio : ActivityCompat.requestPermissions Shows no dialog

Calling the ActivityCompat.requestPermissions opens no dialog. I have been at this for a couple of days and have not been able to find any answer that works on this topic. I have heard of some bugs but nothing in specific to this case.

Android Version on Phone: 6.0.1

All Permissions for storage are accepted in application settings on the test phone. App has been cleared of data twice, as well as uninstalled and re-installed.

It is making it to the requestPermissions() call.

Gradle:

apply plugin: 'com.android.application'

android {
compileSdkVersion 23
buildToolsVersion '25'

defaultConfig {
    applicationId "APPID"
    minSdkVersion 16
    targetSdkVersion 23
    versionCode 1
    versionName "1.0"
}
buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}
}

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.facebook.android:facebook-android-sdk:4.+'
compile 'com.android.support:appcompat-v7:23.4.0'
compile('com.affectiva.android:affdexsdk:3.1.2')

}

Manifest File includes:

<?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="PACKAGE NAME">

<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme"
    tools:replace="android:allowBackup,android:label">
    <activity
        android:name=".LoginActivity"
        android:screenOrientation="portrait">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name="com.facebook.FacebookActivity"
        android:configChanges="keyboard|keyboardHidden|screenLayout|screenSize|orientation"
        android:label="@string/app_name"/>
        <!--android:theme="@android:style/Theme.Translucent.NoTitleBar" -->

    <meta-data
        android:name="com.facebook.sdk.ApplicationId"
        android:value="@string/facebook_app_id" />

    <activity
        android:name=".ProfileActivity"
        android:screenOrientation="portrait"></activity>
    <activity android:name=".QuestionOne"></activity>
</application>

Initially called here:

    if(loggedIn && friendsNames.size() > 0) {

        try{

            verifyStoragePermissions(this);
        }
        catch(Exception e ){
            Log.e("LoginActv: Save Block", e.getMessage());
            e.getStackTrace();
        }

        Intent intent = new Intent(this, QuestionOne.class);
        intent.putStringArrayListExtra("names",friendsNames);
        intent.putStringArrayListExtra("urls", pictureURL);

        startActivity(intent);

and of course permissions are requested here. Note that 'permissions != 1' vs 'permission != PackageManager.PERMISSION_GRANTED' because otherwise, it will not run into requestPermissions:

public static void verifyStoragePermissions(Activity activity) {
    // Check if we have write permission
    int permission = ActivityCompat.checkSelfPermission(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE);

    Log.e("Permission value", permission + " ");
    if (permission != 1) {
        Log.e("Permission ask", "Asking, permission not granted");
        ActivityCompat.requestPermissions(
                activity,
                PERMISSIONS_STORAGE,
                REQUEST_EXTERNAL_STORAGE
        );
    }
}

Permission variables as follows:

// Storage Permissions
private static final int REQUEST_EXTERNAL_STORAGE = 1;
private static String[] PERMISSIONS_STORAGE = {
        Manifest.permission.READ_EXTERNAL_STORAGE,
        Manifest.permission.WRITE_EXTERNAL_STORAGE
};

And finally override of onRequestPermissionsResult() which is never even called.

@Override
public void onRequestPermissionsResult(int requestCode,  String[] permissions, int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);


    if (requestCode == 200) {
        if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {

            Log.e("PERResult", "Granted");
            resultExport file = new resultExport();
            file.setUserName(facebookHandler.getUserName());
            file.setUserID(facebookHandler.getUserID());
            file.addData("friendNames", friendsNames);
            saveObject(file, facebookHandler.getUserName());


        } else {
            Log.e("PERResult", "Denied");
            Toast.makeText(getApplicationContext(), "PERMISSION_DENIED", Toast.LENGTH_SHORT).show();
        }
    }
}

I am at a complete loss on why the dialog will not pop up once I request permissions. Any help would really be appreciated.


EDIT:

Both

int permission = ActivityCompat.checkSelfPermission(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE);

and

int permissionCheck = ContextCompat.checkSelfPermission(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE);

Return 0, however attempting to save gets:

open failed: EACCES (Permission denied)

Upvotes: 0

Views: 2423

Answers (3)

Johnny
Johnny

Reputation: 1964

I encountered the similar question but found the answer.

You must check your merged manifest to make sure your app or a library doesn't have WRITE_EXTERNAL_STORAGE permission with android:maxSdkVersion.

WRITE_EXTERNAL_STORAGE permission with android:maxSdkVersion If you found that android:maxSdkVersion, Replace WRITE_EXTERNAL_STORAGE permission tag to follows:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" tools:node="replace"/>

Will solve the problem.

Upvotes: 0

Lpuche
Lpuche

Reputation: 139

Apparently it needed no dialog because permissions were there. However it threw permissions denied merely because of a path error.

From

ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(new File(Environment.getExternalStorageDirectory().getPath() + name + ".bin")));

to

ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(new File(Environment.getExternalStorageDirectory().getPath()+ "/" + name + ".bin")));

The first example throws a permission error.

Upvotes: 1

Jozef Dochan
Jozef Dochan

Reputation: 935

It should look like this

if (permission != PackageManager.PERMISSION_GRANTED) {...}

because 1 is for PackageManager.SIGNATURE_NEITHER_SIGNED

Upvotes: 0

Related Questions