37

I'm trying to call the contact picker, get the persons name, phone and e-mail into strings and send them to another activity using an intent. So far this works:

Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);  
startActivityForResult(intent, 1);  

// ...

@Override  
public void onActivityResult(int reqCode, int resultCode, Intent data) {  
    super.onActivityResult(reqCode, resultCode, data);  
    if (resultCode == Activity.RESULT_OK) {  
        Uri contactData = data.getData();  
        Cursor c =  managedQuery(contactData, null, null, null, null);  
        if (c.moveToFirst()) {  
            String name = c.getString(c.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME));  
            Intent intent = new Intent(CurrentActivity.this, NewActivity.class);  
            intent.putExtra("name", name);  
            startActivityForResult(intent, 0);  
        }  
    }  
}

But if i add in:

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

it force closes

Maybe theres another way to get their number?

Michael Celey
  • 12,645
  • 6
  • 57
  • 62
MindlessBarber
  • 1,590
  • 6
  • 22
  • 39

2 Answers2

61

Phone Numbers

Phone numbers are stored in their own table and need to be queried separately. To query the phone number table use the URI stored in the SDK variable ContactsContract.CommonDataKinds.Phone.CONTENT_URI. Use a WHERE conditional to get the phone numbers for the specified contact.

    if (Integer.parseInt(cur.getString(
           cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
        Cursor pCur = cr.query(
        ContactsContract.CommonDataKinds.Phone.CONTENT_URI, 
        null, 
        ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?", 
        new String[]{id}, null);
        while (pCur.moveToNext()) {
        // Do something with phones
        } 
        pCur.close();
    }

Perform a second query against the Android contacts SQLite database. The phone numbers are queried against the URI stored in ContactsContract.CommonDataKinds.Phone.CONTENT_URI. The contact ID is stored in the phone table as ContactsContract.CommonDataKinds.Phone.CONTACT_ID and the WHERE clause is used to limit the data returned.

Email Addresses

Querying email addresses is similar to phone numbers. A query must be performed to get email addresses from the database. Query the URI stored in ContactsContract.CommonDataKinds.Email.CONTENT_URI to query the email address table.

Cursor emailCur = cr.query( 
        ContactsContract.CommonDataKinds.Email.CONTENT_URI, 
        null,
        ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?", 
        new String[]{id}, null); 
    while (emailCur.moveToNext()) { 
        // This would allow you get several email addresses
            // if the email addresses were stored in an array
        String email = emailCur.getString(
                      emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
        String emailType = emailCur.getString(
                      emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.TYPE)); 
    } 
    emailCur.close();

As with the phone query the field names for the email table are also stored under ContactsContract.CommonDataKinds. The email query is performed on the URI in ContactsContract.CommonDataKinds.Email.CONTENT_URI and the WHERE clause has to match the ContactsContract.CommonDataKinds.Email.CONTACT_ID field. Since multiple email addresses can be stored loop through the records returned in the Cursor.

More tutorials here

This method requires Android API version 5 or higher.

Kelly Elton
  • 4,373
  • 10
  • 53
  • 97
Pentium10
  • 204,586
  • 122
  • 423
  • 502
  • works great and the website you linked to well deffo come in handy, thanks – MindlessBarber Jun 15 '10 at 14:56
  • 1
    How do i query the phone number in the last while loop? – eyal Jul 10 '11 at 11:23
  • 7
    cr.query doesn't seem to work. Also, what is id in String[]{id}...can't seem to find either of these variables. – Kelly Elton Jul 29 '11 at 01:24
  • Check the tutorial page for full source – Waltsu Dec 13 '11 at 12:47
  • 1
    @kelton52 cr ist content resolver. you can retrieve it using getContentResolver() – DarkLeafyGreen Dec 24 '11 at 15:06
  • 3
    @eyal String phoneNumber = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); – DarkLeafyGreen Dec 24 '11 at 15:10
  • 1
    Don't forget to add android.permission.READ_CONTACTS permission on read contacts – Maxim Shoustin Oct 15 '12 at 21:32
  • 1
    I've found an alternative to avoid multiple queries, using `ContactsContract.Contacts.Entity` here: http://stackoverflow.com/a/14082019/1606534 – Bianca Daniciuc Oct 07 '15 at 07:41
  • While iterating in the while loop while loop getting same mobile numbers twice, when the uri is like this content://com.android.contacts/contacts/lookup/1118i628aae790e73a270.1118i2b0a19068ad54a33.3789r2650-4549375727413B2749.3789r2774-4549375727413B2749.3789r2811-4549375727413B2749.475ig%3A100052505231576357108/831 Whereas getting contact only one contact when uri is like this content://com.android.contacts/contacts/lookup/1118i3d659e210e58e365/754 Any idea to getting nos only once associated with that contact – Mukesh Nov 15 '15 at 08:06
1

Building on the accepted answer, if you want to jump straight to the desired email address and not require the contacts permission use something like this:

private static final int REQUEST_CODE_EMAIL = 1;

void startSelectingEmail() {
    Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.CommonDataKinds.Email.CONTENT_URI);
    startActivityForResult(intent, REQUEST_CODE_EMAIL);
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == Activity.RESULT_OK && requestCode == REQUEST_CODE_EMAIL) {
        Uri emailUri = data.getData();
        Cursor emailCursor = getContext().getContentResolver().query(emailUri, null, null, null, null);
        if (emailCursor != null) {
            if (emailCursor.moveToFirst()) {
                String email = emailCursor.getString(
                        emailCursor.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Email.DATA));
                String emailType = emailCursor.getString(
                        emailCursor.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Email.TYPE));
                Log.d(TAG, "Email: " + emailType + " " + email);
            }
            emailCursor.close();
        }
    }
}

This doesn't require the contacts permission to read the email address like the double query methods above. It also makes it so that you do not need to write UI for the user to select the appropriate email address for contacts with multiple emails, the user selects a specific email in the Contacts app so you only get one result.

The cursor comes back with quite a few columns in addition to just email address like display name, though that has only been verified on a Nexus 5 running Android M.

caller9
  • 2,207
  • 1
  • 18
  • 11