6

I am trying to get name and email-id from Android inbuilt phone book into my page, I am able to get name, contact ID, phone number. but I am unable to get email ID from the Android phone book.

Code is:

 public static final int PICK_CONTACT = 1;
 @Override
 button.setOnClickListener(new OnClickListener() {
 public void onClick(View _view) {
 Intent intent = new Intent(Intent.ACTION_PICK,ContactsContract.Contacts.CONTENT_URI);
 startActivityForResult(intent, PICK_CONTACT);  
   } 
 });
}

@Override 
public void onActivityResult(int reqCode, int resCode, Intent data) 
{
 super.onActivityResult(reqCode, resCode, data);
    
switch(reqCode) {
  case (PICK_CONTACT) : {
    if (resCode == Activity.RESULT_OK) {
      Uri contactData = data.getData();
      Cursor c = managedQuery(contactData, null, null, null, null);
      c.moveToFirst();
      String name = c.getString(c.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME));
      String name1 = c.getString(c.getColumnIndexOrThrow(ContactsContract.Contacts.HAS_PHONE_NUMBER));
      String ContactID = c.getString(c.getColumnIndex(ContactsContract.Contacts._ID));

   if(Integer.parseInt(name1) == 1){
     Cursor emails = getContentResolver().query(ContactsContract.CommonDataKinds.Email.CONTENT_URI,null,
     ContactsContract.CommonDataKinds.Email.CONTACT_ID+ " = " + ContactID, null, null);
     TextView tv = (TextView)findViewById(R.id.selected_contact_textview);
     TextView tv1 = (TextView)findViewById(R.id.selected_email_textview);
     tv.setText(name);
     tv1.setText(ContactID);
      }
      }
        break;
  }
 } 

Here I am able to get name and contact ID of a selected person from the phonebook. Now I want to get name and email ID of a selected person from phone book. How can I achieve this?

Community
  • 1
  • 1
  • 3
    refer this link: http://stackoverflow.com/questions/6152442/how-to-get-contact-email-id – Vineet Shukla Nov 21 '11 at 06:33
  • @Vineet Shukla thanks for reply, i am trying to get emailID directly from android phone book as i am taking name. i have a code which takes email-id and name from phonebook into my own listview, but that code works fine on Emulator not on actual device. –  Nov 21 '11 at 06:53
  • @Brock Adams thanks for Edit the Code. I got the solution. –  Nov 21 '11 at 07:04
  • 1
    You're welcome, but actually @Vivek did most of the editing, I just approved it and fixed one overlooked revision. ... ... If you have the solution, then post it as an answer to this Q. – Brock Adams Nov 21 '11 at 07:13

1 Answers1

0

You can get the email address of the contact directly using the contact id as follows :

    String selection = ContactsContract.CommonDataKinds.Email.CONTACT_ID + " =? ";
    String[] selectionArgs = new String[]{aContactId};
    Cursor emailCursor = aContext.getContentResolver().query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, null, selection, selectionArgs, null);
    while (!emailCursor.isAfterLast())
    {
        emailString = emailCursor.getString(emailCursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
// do something with email string
        emailCursor.moveToNext();
    }
    emailCursor.close ();
ShReYaNsH
  • 302
  • 2
  • 10