从ContactsContract.Contacts获取数据

时间:2011-06-26 01:11:01

标签: android contacts

我在模拟器中有一个地址簿,其中包含一个实体。

我为联系人创建联系人选择器:

Intent intent = new Intent(Intent.ACTION_PICK);
intent.setData(ContactsContract.CONTENT_URI);
startActivityForResult(intent, MY_CODE);

我选择了地址簿中的单个条目,然后我找回了一个具有以下数据的意图:

content://com.android.contacts/contacts/lookup/0n354B31353539292D4553313F3F39/1/

为ContactsContract.Contacts冷却它是一个CONTENT_LOOKUP_URI。我可以立即使用它来直接访问ContactsContract.Contacts中的内容,例如显示名称。太棒了。好了,现在假设我想从该用户的ContactsContract.Contacts.Data或ContactsContract.Contacts.Photo获取数据。阅读ContactsContract.Contacts.Photo / Data中的CONTENT_DIRECTORY条目的文档:

This directory can be used either with a CONTENT_URI or CONTENT_LOOKUP_URI.

所以我尝试了什么是合乎逻辑的:

uri = Uri.withAppendedPath(uri, ContactsContract.Contacts.Photo.CONTENT_DIRECTORY);
Cursor c = myContentResolver.query(uri, null, null, null); // simplified
...

现在我收到一个令人讨厌的错误:

java.lang.IllegalArgumentException: Unknown URL content://com.android.contacts/contacts/lookup/0n354B31353539292D4553313F3F39/1/photo

到底是什么? 联系人有一个显示名称,是的,它有一张照片。为什么这被认为是错误的?到目前为止,我已经在2.1和2.2上尝试了这个。

Documentation for ContactsContract.Contacts.Photo

2 个答案:

答案 0 :(得分:1)

答案很晚但我希望有人能发现它有用。

对于android< 3.0它没有用。请改用_ID:

Uri contactUri;
//if at least HC uses lookupUri which is more efficient; for previous versions does not work
if (AT_LEAST_HONEYCOMB) {
    contactUri = getLookupUri(contactId, contactLookupKey);
} else {
    contactUri = Uri.withAppendedPath(Contacts.CONTENT_URI, "" + contactId);
}
Uri contactDetailsUri = Uri.withAppendedPath(contactUri, Contacts.Data.CONTENT_DIRECTORY);

// contactDetailsUri android<3.0: com.android.contacts/contacts/1/data 
//contactDetailsUri android>=3.0: com.android.contacts/contacts/lookup/0n354B31353539292D4553313F3F39/1/data



Cursor detailsCursor = getActivity().getContentResolver().query(
                    contactDetailsUri,
                    PROJECTION,
                    null,
                    null,
                    SORT_ORDER);

PS:抱歉丑陋的代码

答案 1 :(得分:0)

使用此代码从ContactsContract.Contacts.CONTENT_URI

获取联系人的 PhoneNumber
    String contactID = null;
    Cursor cursor = context.getContentResolver().query(uri,
            new String[]{ContactsContract.Contacts._ID},
            null, null, null);

    if (cursor.moveToFirst()) {

        contactID = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
    }

    cursor.close();

    // Using the contact ID now we will get contact phone number
    Cursor cursorPhone = context.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
            new String[]{ContactsContract.CommonDataKinds.Phone.NUMBER},

            ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ? AND " +
                    ContactsContract.CommonDataKinds.Phone.TYPE + " = " +
                    ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE,

            new String[]{contactID},
            null);

    if (cursorPhone.moveToFirst()) {
        phoneNo = cursorPhone.getString(cursorPhone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
    }

    cursorPhone.close();