Pick contact directly from contact picker intent

20,194

Solution 1

I also had the same problem. Finally, I got rid of intermediate picker screen using below code,

Intent i=new Intent(Intent.ACTION_PICK);
i.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE);
startActivityForResult(i, SELECT_PHONE_NUMBER);

In onActivityResult get phone number as below

if (requestCode == SELECT_PHONE_NUMBER && resultCode == RESULT_OK) {
  // Get the URI and query the content provider for the phone number
  Uri contactUri = data.getData();
  String[] projection = new String[]{ContactsContract.CommonDataKinds.Phone.NUMBER};
  Cursor cursor = getContext().getContentResolver().query(contactUri, projection,
      null, null, null);

  // If the cursor returned is valid, get the phone number
  if (cursor != null && cursor.moveToFirst()) {
    int numberIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
    String number = cursor.getString(numberIndex);
    // Do something with the phone number
    ... 
  } 

  cursor.close();
}

Solution 2

Try the below code to pick contact:

Intent contactPickerIntent = new Intent(Intent.ACTION_PICK,
                    ContactsContract.CommonDataKinds.Phone.CONTENT_URI);
            startActivityForResult(contactPickerIntent, RESULT_PICK_CONTACT);

You can fetch the required information in onActivityResult as follows:

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == RESULT_OK) {
        switch (requestCode) {
            case RESULT_PICK_CONTACT:
                  Cursor cursor = null;
    try {
        String phoneNo = null;
        String name = null;

        Uri uri = data.getData();
        cursor = getContentResolver().query(uri, null, null, null, null);
        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("Name and Contact number is",name+","+phoneNo);

    } catch (Exception e) {
        e.printStackTrace();
    }
                break;
        }
    } else {
        Log.e("Failed", "Not able to pick contact");
    }
}

Solution 3

Here is a way to pick name, phone number from Contact in Kotlin

private fun pickEmergencyFromContacts() {
    val i = Intent(Intent.ACTION_PICK)
    i.type = ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE
    startActivityForResult(i, SELECT_PHONE_NUMBER)
}

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
    super.onActivityResult(requestCode, resultCode, data)
    if (requestCode == SELECT_PHONE_NUMBER && resultCode == Activity.RESULT_OK) {
        val contactUri = data?.data ?: return
        val projection = arrayOf(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
                ContactsContract.CommonDataKinds.Phone.NUMBER)
        val cursor = requireContext().contentResolver.query(contactUri, projection,
                null, null, null)

        if (cursor != null && cursor.moveToFirst()) {
            val nameIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME)
            val numberIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)
            val name = cursor.getString(nameIndex)
            val number = cursor.getString(numberIndex)

            // do something with name and phone
        }
        cursor?.close()
    }
}

companion object {

    private const val SELECT_PHONE_NUMBER = 111    
    ...
}

Hope it help

Solution 4

For Android 11+, need to add below code in AndroidManifest

......
</application>
    <queries>
        <intent>
            <action android:name="android.intent.action.VIEW" />
            <category android:name="android.intent.category.DEFAULT" />
            <data android:mimeType="vnd.android.cursor.dir/contact" />
        </intent>
    </queries>
</manifest>

Solution 5

This works for me:

Intent it= new Intent(Intent.ACTION_PICK, 
     ContactsContract.Contacts.CONTENT_URI); 
startActivityForResult(it, requestCode);
Share:
20,194

Related videos on Youtube

Hardik Trivedi
Author by

Hardik Trivedi

A computer program writer, A blogger. An author. Usually I do a lot of Android Stuff.

Updated on July 09, 2022

Comments

  • Hardik Trivedi
    Hardik Trivedi almost 2 years

    Hello I want to pick a contact from our default contact book intent. I tried several ways to do it. Please find the code below. The problem with all those code is that they open one intermediate documents screen with few options there user has to select contact and than it opens contact book.

    private void openContactIntent() {
         Intent intent = new Intent(Intent.ACTION_GET_CONTENT, ContactsContract.Contacts.CONTENT_URI);
         intent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE);
         startActivityForResult(intent, REQ_CONTACT_DIRECTORY);
    }
    

    I also tried

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

    and

    Intent intent = new Intent(Intent.ACTION_PICK);
    intent.setType(ContactsContract.Contacts.CONTENT_TYPE);
    startActivityForResult(intent, PICK_CONTACT); 
    

    What I see as an intermediate screen is enter image description here

    • android_griezmann
      android_griezmann over 7 years
      Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI); startActivityForResult(intent, PICK_CONTACT); It's working for me!
    • Hardik Trivedi
      Hardik Trivedi over 7 years
      Is it related to any OS ? I am running code on Android N. And for me it not working. I have not added any permissions.
    • android_griezmann
      android_griezmann over 7 years
      I am running Android N too!
    • Hardik Trivedi
      Hardik Trivedi over 7 years
      And permissions ?
    • Hardik Trivedi
      Hardik Trivedi over 7 years
      This is opening a side menu screen like attached screenshot in phone and not even showing contact inside it.
    • android_griezmann
      android_griezmann over 7 years
      No permission needed! Paste your targetSdkVersion
    • android_griezmann
      android_griezmann over 7 years
    • Hardik Trivedi
      Hardik Trivedi over 7 years
      I am able to solve the problem. There is some catch with the request code. I was using request code 10. The moment I use some odd number like 8500 it started working fine. Anyone knows the reason?
    • Maifee Ul Asad
      Maifee Ul Asad about 4 years
  • Android Geek
    Android Geek over 6 years
    oH..!! It's great..!!
  • Silvia H
    Silvia H over 5 years
    This actually still opens the file manager on some devices, better also add intent.setType(ContactsContract.CommonDataKinds.Phone.CONTEN‌​T_TYPE);
  • Trevor Hart
    Trevor Hart almost 5 years
    This worked well for me except getContext needed to be replaced with a reference to the activity (this), otherwise I got a no instrumentation registered issue.
  • Taha Kirmani
    Taha Kirmani over 4 years
    This only gives 1 contact, is there a way to select multiple contacts in one go?
  • Ganesan J
    Ganesan J over 3 years
    here what is RESULT_PICK_CONTACT . it is string or other variable ?
  • Ohad Cohen
    Ohad Cohen over 3 years
    @Ganesan J It's an int, have a look at Getting a result from an activity article
  • behelit
    behelit about 3 years
    Works well but the result code is random for some reason, could be an Android 11 issue
  • Somnath Kadam
    Somnath Kadam almost 3 years
    For Android 11+, changes in AndroidManifest file like stackoverflow.com/a/67600033/1889825
  • devDeejay
    devDeejay about 2 years
    Do you know how to call the pending intents using the registerActivityForResult. I am stuck with a compile-time error where .launch() function is expecting a pending intent.
  • Thirumalvalavan
    Thirumalvalavan about 2 years
    I didn't try with pending intent. Below link answer may help to you, stackoverflow.com/questions/10362525/…