20

I found on SO that to launch a filtered version of contact picker (which only shows contacts that have phone numbers), I can just use this:

Intent pickContactIntent = new Intent( Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI );
pickContactIntent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE);
startActivityForResult(pickContactIntent, CONTACT_PICKER_RESULT);

So this works. I'm just trying to figure out how to retrieve the name and phone number of the selected contact now, within the onActivityResult method:

@Override  
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
     // what goes here...
}

I've tried a number of different things inside onActivityResult, but the queries don't return the number.

user1202422
  • 558
  • 2
  • 8
  • 16
  • possible duplicate of [Pick a Number and Name From Contacts List in android app](http://stackoverflow.com/questions/9496350/pick-a-number-and-name-from-contacts-list-in-android-app) – flx Sep 01 '13 at 14:54
  • when i open the contact picker like this , the cursor i get is always empty. What can it be ? – kommradHomer May 17 '14 at 22:48

2 Answers2

32
Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.CommonDataKinds.Phone.CONTENT_URI);  
startActivityForResult(intent, 1);

String phoneNo = null;
Uri uri = data.getData();
Cursor cursor = getContentResolver().query(uri, null, null, null, null);

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

curosr.close();
Saket
  • 2,945
  • 1
  • 29
  • 31
Akash Yadav
  • 861
  • 7
  • 11
9

Building on Akash's answer.

Step 1 - need to add Read/Write contact permissions in Manifest.

<uses-permission android:name="android.permission.READ_CONTACTS"/>
<!-- OR - depends on the requiremnts-->
<uses-permission android:name="android.permission.WRITE_CONTACTS"/>

Step 2 - then need to launch contact picker intent, but before that need to check if Contact permission is granted (only for Android >=23)

if (hasPermissions(Manifest.permission.READ_CONTACTS)) {
      Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.CommonDataKinds.Phone.CONTENT_URI);
      startActivityForResult(intent, REQUEST_PICK_CONTACT);
} else {
     // Request Permissions
}

Step 3 - then need to get Activity Result

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
     super.onActivityResult(requestCode, resultCode, data);
     if (resultCode == Activity.RESULT_OK && requestCode == REQUEST_PICK_CONTACT) {
         String phoneNo = null;
         String name = null;

         Uri uri = data.getData();
         Cursor cursor = getActivity().getContentResolver().query(uri, null, null, null, null);

         if (cursor.moveToFirst()) {
         int phoneIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
         int nameIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);

         phoneNo = cursor.getString(phoneIndex);
         name = cursor.getString(nameIndex);

         Log.e("onActivityResult()", phoneIndex + " " + phoneNo + " " + nameIndex + " " + name);
      }
      cursor.close();
  }
}

And yeah thats it.

Noman Rafique
  • 3,735
  • 26
  • 29