Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

permission.READ_CONTACTS does not seem to work

Tags:

I'm working on a simple app that browses through the user's contacts. Unfortunately I keep getting the following error:

java.lang.SecurityException: Permission Denial: reading com.android.providers.contacts.HtcContactsProvider2 uri content://com.android.contacts/contacts from pid=27455, uid=10171 requires android.permission.READ_CONTACTS

My manifest file looks like this:

<?xml version="1.0" encoding="utf-8"?>  
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.android.helloMaps"
    android:versionCode="1"
    android:versionName="1.0">
    <uses-sdk android:minSdkVersion="8" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="android.permission.READ_CONTACTS" />   
    <application android:icon="@drawable/icon" 
        android:label="@string/app_name" 
        android:debuggable="true">
        <uses-library android:name="com.google.android.maps" />
        <activity android:name=".MapsActivity"
                  android:label="@string/app_name"
                  android:theme="@android:style/Theme.NoTitleBar">            
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>

        </activity>
    </application>
</manifest>

I'm trying to look at my contacts by:

 Cursor cursor = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI,null, null, null, null);  

I've tried to add <uses-permission android:name="android.permission.READ_CONTACTS" /> or android:permission="android.permission.READ_CONTACTS" to <application> or <activity>, but both didn't work.

I'm running out of options, does anybody know what I'm missing here?

like image 841
Danielvd Avatar asked Nov 12 '11 13:11

Danielvd


People also ask

How do Android permissions work?

App permissions help support user privacy by protecting access to the following: Restricted data, such as system state and a user's contact information. Restricted actions, such as connecting to a paired device and recording audio.

What are manifest permissions?

The Android manifest file helps to declare the permissions that an app must have to access data from other apps. The Android manifest file also specifies the app's package name that helps the Android SDK while building the app.


2 Answers

the <uses-permission> should be contained in the <manifest> element. See Structure of the Manifest File. So trying putting it into <application> or <activity>, won't work. Desperation move: try to move <uses-sdk> before <application>

<uses-sdk android:minSdkVersion="8" />
<uses-permission android:name="android.permission.READ_CONTACTS" />

Also if you can test without the other permissions, remove them.

EDIT: Can you please check if this is your TOP line in your manifest file:

<?xml version="1.0" encoding="utf-8"?>
like image 128
hovanessyan Avatar answered Oct 09 '22 02:10

hovanessyan


I was totally stuck on this until I read this article about how permissions are handled starting with SDK 23. The critical hint:

If the application's targetSdkVersion is set to less than 23. It will be assumed that application is not tested with new permission system yet and will switch to the same old behavior: user has to accept every single permission at install time and they will be all granted once installed !

I opened up my gradle.build file and changed targetSdkVersion 23 to targetSdkVersion 22, and now everything works great! I'll eventually find time to build in the new permissions system and switch back to targetSdkVersion 23, which is probably the more correct answer. This is a temporary shortcut.

like image 37
Chase Street Dev Avatar answered Oct 09 '22 00:10

Chase Street Dev