选择与电子邮件联系

时间:2013-12-03 11:48:55

标签: android android-intent android-contentprovider android-contacts

我使用以下意图从联系人应用中选择一个电子邮件联系人:

Intent selectContact = new Intent(Intent.ACTION_PICK, ContactsContract.CommonDataKinds.Email.CONTENT_URI);
        selectContact.setType(ContactsContract.CommonDataKinds.Email.CONTENT_TYPE);
        startActivityForResult(selectContact, RESULT_PICK_CONTACT);

在活动结果上,我尝试查询返回的ID,如下所示:

Uri contactData = intent.getData();
        String contactId = contactData.getLastPathSegment();
        Cursor cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Email.CONTENT_URI,
                null,
                ContactsContract.CommonDataKinds.Email.CONTACT_ID.concat(" = ?"),
                new String[]{
                        contactId
                },
                null);

不幸的是,我用来查询已挑选的联系人的Cursor总是为空,尽管返回的联系人ID似乎是有效的。

我做错了什么?

1 个答案:

答案 0 :(得分:1)

查询不同的ID解决了问题。

选择电子邮件联系人时,CONTACT_ID常量似乎不是正确的标识符。

我将Selection参数更改为使用_ID代替:

ContactsContract.CommonDataKinds.Email._ID.concat(" = ?")