Android pick email intent

2019-03-27 08:18发布

问题:

I'd like to pick an email from the contacts list. Picking a contact is not good enough, because a contact can have several emails.

Using the API demo, I managed to pick a contact, phone number and even an address. Example:

Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType(ContactsContract.Contacts.CONTENT_ITEM_TYPE);
// OR
intent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE);
// OR
intent.setType(ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE);

BUT, when trying to pick an email

intent.setType(ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE);

I get activity not found exception.

Any idea on how to pick an email from the all the contacts' emails?

Thanks. Alik.

Update (2011/05/02): Found another way to pick things from the contacts but still the email picker is not registered to the intent.

Working:

new Intent(Intent.ACTION_PICK,ContactsContract.Contacts.CONTENT_URI);
new Intent(Intent.ACTION_PICK,ContactsContract.CommonDataKinds.Phone.CONTENT_URI);
new Intent(Intent.ACTION_PICK,ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_URI);

NOT working:

new Intent(Intent.ACTION_PICK, ContactsContract.CommonDataKinds.Email.CONTENT_URI);

回答1:

I haven't specifically tried to use a picker, but we loop through our cache of the contacts and find all the contact details with a MIME type of Email.CONTENT_ITEM_TYPE.

Then we build a dialog to the let user pick which e-mail address they want to use, and we pass that e-mail address to the user's e-mail app, e.g.

  final Intent emailIntent = new Intent(Intent.ACTION_SEND);
  emailIntent.putExtra(Intent.EXTRA_STREAM, "Some text");

  Builder builder = new Builder(this);
  builder.setTitle("Choose email");
  builder.setItems(emailAddressesArray, new DialogInterface.OnClickListener()
  {
    @Override
    public void onClick(DialogInterface dialog, int which)
    {
      String address = emailAddresses.get(emailAddressesArray[which]);
      sLog.user("User selected e-mail: " + address);
      emailIntent.putExtra(Intent.EXTRA_EMAIL, new String[]{address});
      startExternalActivity(emailIntent);
    }
  });
  builder.show();


回答2:

you have to use the following constant (not CONTENT_ITEM_TYPE):

intent.setType(ContactsContract.CommonDataKinds.Email.CONTENT_TYPE);


回答3:

Here's a sample bit of code to display all email addresses in the contact list to the user and allow them to select a single one (which is then put into an EditText with the id of R.id.youredittextid).

Note: This is a rather inefficient way to do this, and will cause quite a delay if you have lots of contacts. But all the necessary code is here; customize as you see fit...

        // We're going to make up an array of email addresses
        final ArrayList<HashMap<String, String>> addresses = new ArrayList<HashMap<String, String>>();

        // Step through every contact
        final ContentResolver cr = getContentResolver();
        final Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
        while (cursor.moveToNext())
        {
            final String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
            final String contactName = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));

            // Pull out every email address for this particular contact
            final Cursor emails = cr.query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, null, 
                                            ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = " + contactId, null, null);
            while (emails.moveToNext()) 
            {
                // Add email address to our array
                final String strEmail = emails.getString(emails.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));

                final HashMap<String, String> email = new HashMap<String, String>();
                email.put("Name", contactName);
                email.put("Email", strEmail);

                addresses.add(email);
            }
            emails.close();
        }

        // Make an adapter to display the list
        SimpleAdapter adapter = new SimpleAdapter(this, addresses, android.R.layout.two_line_list_item,
                                                    new String[] { "Name", "Email" },
                                                    new int[] { android.R.id.text1, android.R.id.text2 });

        // Show the list and let the user pick an email address
        new AlertDialog.Builder(this)
          .setTitle("Select Recipient")
          .setAdapter(adapter, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {

                EditText e = (EditText)findViewById(R.id.youredittextid);
                HashMap<String, String> email = addresses.get(which);
                e.setText(email.get("Email"));

              dialog.dismiss();
            }
          }).create().show(); 


回答4:

An old thread but... this information could prove useful to someone. When you start an Intent with Intent.ACTION_PICK you are trying to invoke the "Contact Picker" activity, which is usually supplied by Contacts / Phonebook application.

Latest version of vanilla (Google) Contacts app (Android 4.4.4) does have Email.CONTENT_ITEM_TYPE for mimetype in its intent filter, so it should respond to such intent, just as you made it. I am not sure but it seems that Contact Picker for older versions (ICS, JB) did not have this in its intent filters.

In short, this intent should work on KK with vanilla Contacts installed, and should not work on older Android versions.



回答5:

Perfectly working:

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

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 1) {
            if (resultCode == Activity.RESULT_OK) {
                Uri contactData = data.getData();
                Cursor cursor = getActivity().managedQuery(contactData, null, null, null, null);
                cursor.moveToFirst();
                String email = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.NUMBER));
                editText.setText(email);
            }


        }
}