22

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?

Jimi
  • 539
  • 5
  • 21
Danielvd
  • 341
  • 1
  • 2
  • 4

6 Answers6

18

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"?>
hovanessyan
  • 30,580
  • 6
  • 55
  • 83
  • thanks for you answer, I first moved `` all the way up, that didn't help. Next I removed all permissions except for the READ_CONTACTS, unfortunately that didn't work either – Danielvd Nov 12 '11 at 14:36
  • Hi, I edited my answer. I am not sure if you have posted all of your manifest, or only part. In any case, the permission declaration is correct. Can you try to run "validate" on your manifest? – hovanessyan Nov 12 '11 at 14:41
  • my bad, I intented to post my entire manifest, apparently I forgot the first line. I edited my question and added the first line. – Danielvd Nov 12 '11 at 15:00
  • I ran "validate", it says "finished with one warning". Something about "No grammar constraints". Don't think that can cause this behavior – Danielvd Nov 12 '11 at 15:08
  • you're right, this should be no problem. Well, have you tried to clean the project, restart the emulator/IDE? – hovanessyan Nov 12 '11 at 15:22
  • tried it on phone and on emulater, both gave problems. I did a re-install of all SDK software, now it works! Hovanessyan thanks for all the effort! – Danielvd Nov 12 '11 at 20:21
10

None of the above helped. The solution is quite simple. you'll need a runtime permission request.

with or without placing the permission in your manifest, You will need to request that permission from the User on-the-fly.

if( getApplicationContext().checkSelfPermission( Manifest.permission.READ_CONTACTS ) != PackageManager.PERMISSION_GRANTED )
     ActivityCompat.requestPermissions(activity, new String[]{Manifest.permission.READ_CONTACTS}, resultValue);

only then after the approval you will be able to query the contacts phone number/name etc.

Aykhan Hagverdili
  • 28,141
  • 6
  • 41
  • 93
Alon Kogan
  • 3,258
  • 1
  • 21
  • 20
9

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.

  • 1
    This was it for me, thanks! (The error message in the exception thrown by Android was not very useful.) We chose to properly implement requesting runtime permissions, and for that the [official docs](https://developer.android.com/training/permissions/requesting.html) were ultimately the most useful resource. – Jonik Jun 16 '16 at 09:08
  • "Google Play will require that new apps target at least Android 8.0 (API level 26) from August 1, 2018, and that app updates target Android 8.0 from November 1, 2018." so this is a hack that won't work for new apps. A better option would be to use what Alon Kogan wrote, and handle permission requests at runtime. see https://developer.android.com/training/permissions/requesting – Maverick Meerkat Oct 11 '18 at 12:46
7

Try this in your on create method

ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.READ_CONTACTS},1);
Aykhan Hagverdili
  • 28,141
  • 6
  • 41
  • 93
TheChosenOne
  • 71
  • 1
  • 1
3

I had the same problem and none of the solutions I read helped to resolve it until I added the WRITE_EXTERNAL_STORAGE permission along with the READ_CONTACTS permission.

<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Spooky
  • 2,966
  • 8
  • 27
  • 41
user1091524
  • 865
  • 1
  • 15
  • 28
0

I ran into this issue today and none of the above fixes worked for me.

What the issue ended up being had to do with my Eclipse. What I had written in the manifest.xml and what was in the Permissions tab weren't the same. I had the READ_CONTACTS permission in my manifest.xml, but READ_PROFILE was in it's place under my permission's tab.

I know this is an old thread, but hopefully if someone has the same issue I did they'll stumble across this.

Thanks!

AlexB
  • 7,302
  • 12
  • 56
  • 74