Search code examples
javaandroidpermissionscursoruri

"java.lang.SecurityException: Permission Denial" Despite Permissions in Contact Access Android


I am attempting to access and retrieve all of the phone numbers for a particular contact (not all contacts), and am continuously running into the same permissions error despite having properly added necessary permissions.

  1. I restarted Android Studio
  2. I checked the syntax of my declaration
  3. I checked for duplicate activity declarations

Yet the problem persists.

My log is shown here:

java.lang.SecurityException: Permission Denial: reading com.android.providers.contacts.ContactsProvider2 uri content://com.android.contacts/data/phones from pid=30288, uid=10127 requires android.permission.READ_CONTACTS, or grantUriPermission()

And apparently the permission issue occurs at the indicated line in this setup:

if (reqCode == REQUEST_CONTACT_PICKER) {
        if (resultCode == Activity.RESULT_OK) {
            if (data != null) {
                Uri contactData = data.getData();

                try {

                    String id = contactData.getLastPathSegment();
                    Cursor phoneCur = getContentResolver()
                            .query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, //----- -This one- ---------
                                    null,
                                    ContactsContract.CommonDataKinds.Phone.CONTACT_ID
                                            + " = ?", new String[] { id },
                                    null);

For reference here is my manifest, the issue is occurring in Text_main.

<?xml version="1.0" encoding="utf-8"?>

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <uses-permission android:name="android.permission.READ_CONTACTS"/>
    <activity
        android:name=".StartMenu"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name=".Text_main"
        android:label="@string/title_activity_text_main" >
    </activity>
    <activity
        android:name=".Call_main"
        android:label="@string/title_activity_call_main" >
    </activity>
</application>

I am probably making a stupid and/or beginner's error as I am mostly splicing snippets (I am out of my basic comfort zone). I appreciate any help and/or constructive criticism.

By the way my code for retrieving the contacts was adapted from the top answer posted here: All phone numbers of one contact for startActivityForResult


Solution

  • <uses-permission> needs to be an immediate child of <manifest>, as a peer to <application>. It cannot be a child of <application>.