1

i am trying to build a app, where phone contacts only with email address has to be displayed to the user.When the user click on the editbox in my app,phone contacts only with email address has to be displayed,after the user selects a contact, the email address of that contact has to be sent back to my app, which i use it further in my app.

//this is my code under onActivityResult() method

          try 
            {

                Uri result = data.getData();  
                String id = result.getLastPathSegment();  
                cursor = getContentResolver().query(Email.CONTENT_URI, null, Email.CONTACT_ID + "=?", new String[] { id }, null);  
                emailIdx = cursor.getColumnIndex(Email.DATA);

                if (cursor.moveToFirst()) 
                {
                    while (cursor.isAfterLast() == false)
                    {
                        emailid = cursor.getString(emailIdx);

                        allids.add(emailid);
                        cursor.moveToNext();
                    }
                } 

                else 
                {
                    //no results actions
                }  
            } 

// This is the intent i am passing.

Intent intent = new Intent(Intent.ACTION_PICK,ContactsContract.Contacts.CONTENT_URI);
                intent.setType(ContactsContract.CommonDataKinds.Email.ADDRESS);

                  startActivityForResult(intent, 1);

// manifest permissions.

            <uses-permission android:name="android.permission.READ_CONTACTS" />
            <uses-permission android:name="android.permission.GET_ACCOUNTS" />


            <intent-filter>

            <action android:name="android.intent.action.PICK" />
            <category android:name="android.intent.category.DEFAULT" />
            <data android:mimeType="vnd.android.cursor.dir/email_v2" />
            <data android:mimeType="vnd.android.cursor.item/email_v2" />

            </intent-filter>

i am getting the below error when i try to run my app.

 android.content.ActivityNotFoundException: No Activity found to handle Intent {act=android.intent.action.PICK typ=data1 }         

I am not sure of what could be the problem, am i missing something in the manifest.xml?.please help.

Thanks!

DevAndro
  • 205
  • 1
  • 6
  • 18
  • ,Have u got any solution for above problem? I am also getting same problem.If we use below solution it will show all contacts in mobile. – KCRaju Aug 03 '13 at 14:57

2 Answers2

1

This is quite old now but if I was searching for it I'm sure others are too so here's my solution. The intent is launched as follows:

    Intent intent = new Intent(Intent.ACTION_PICK);
    intent.setType(ContactsContract.CommonDataKinds.Email.CONTENT_TYPE);
    startActivityForResult(intent, RC_GET_EMAIL);

Then to get the email address from the picked contact:

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if(requestCode == RC_GET_EMAIL) {
        if(resultCode == Activity.RESULT_OK) {
            Uri contactUri = data.getData();
            String[] projection = new String[]{ContactsContract.CommonDataKinds.Email.DATA};
            Cursor cursor = getContext().getContentResolver().query(contactUri, projection, null, null, null);
            if(cursor != null) { 
                if(cursor.moveToFirst()) {
                    int emailIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA);
                    String email = cursor.getString(emailIndex);
                    // do something with email
                }
                cursor.close();
            }
        }
    }
}

Remember that for Marshmallow and above you will need to manually request the READ_CONTACTS permission in order to do anything meaningful.

Newtz
  • 1,333
  • 11
  • 9
-2

Put the below code in the listener, hope this helps.

Code

Intent intent = new Intent(Intent.ACTION_PICK,Contacts.CONTENT_URI);
startActivityForResult(intent, PICK_CONTACT);
Storm
  • 684
  • 9
  • 20