无法在HTC设备中获取联系人

时间:2015-05-27 14:56:59

标签: android android-intent android-permissions android-contacts

我从各种设备获取ContactPicker的联系人但是在尝试从HTC设备获取联系时我得到了以下异常:

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()

我给出的权限是:

android.permission.READ_CONTACTS

以下是我实施的代码:

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();

            }
        }

    }

感谢任何帮助。

这是我的Android清单文件: -

<?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>

1 个答案:

答案 0 :(得分:1)

尝试在清单中添加以下权限

.fix_block{height:350px}

您的联系人可能会被放置在某些外部存储设备上,这就是它抛出此类例外的原因。