Search code examples
androidandroid-intentandroid-permissionsandroid-contacts

Unable to get the Contacts in HTC devices


I'm getting contacts from the ContactPicker from various devices but While trying to fetch contact from HTC devices I'm gettting the below exception :

java.lang.SecurityException: Permission Denial: reading com.android.providers.contacts.HtcContactsProvider2 uri content://com.android.contacts/data/719 from pid=8344, uid=10214 requires android.permission.READ_CONTACTS, or grantUriPermission()

Permissions that I have given is:

android.permission.READ_CONTACTS

Below is the code that I have implemented:

Intent intent = new Intent(Intent.ACTION_PICK,
                        Phone.CONTENT_URI);
intent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE);
                startActivityForResult(intent, 101);


@Override
    protected void onActivityResult(int arg0, int arg1, Intent arg2) {
        // TODO Auto-generated method stub
        super.onActivityResult(arg0, arg1, arg2);

        if (arg1 == Activity.RESULT_OK) {
            Uri contactData = arg2.getData();
            ContentResolver cur = getContentResolver();
            Cursor cursorID = getContentResolver().query(contactData,
                    new String[] { ContactsContract.Contacts._ID }, null, null,
                    null);

            grantUriPermission("ContactsContract.CommonDataKinds.Phone.CONTENT_URI", contactData, 2);
            Cursor c = cur.query(contactData, null, null, null, null);
            if (c.moveToFirst()) {
                String name = c
                        .getString(c
                                .getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));

                String number = c
                        .getString(c
                                .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));

                Toast.makeText(getApplicationContext(),
                        "" + name + " " + number, 4000).show();

            }
        }

    }

Any help is appreciated.

here is my Android manifest file:-

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.readconatc"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="21" />

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

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

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

Solution

  • Try adding below permission in your manifest

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

    Your contacts may be placed on some external storage that's why it is throwing such a exception.