Search code examples
androidandroid-intentandroid-contentproviderandroid-contacts

Picking Contact with Email


I use the following intent to pick an email-contact from the contacts app.:

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

On activity result i try to query the returned id like this:

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

Unfortunally the Cursor i use to query the picked contact is always empty although the contact id returned seems to be valid.

What am i doing wrong?


Solution

  • Querying onto a different id solved the problem.

    When picking Email contacts the CONTACT_ID constant doesnt seem to be the right identifier.

    I changed Selection argument to use _ID instead:

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