nr1
nr1

Reputation: 777

Android: How to share a contact via intent?

how can i share a Android contact using an intent?

I tried like this, but somehow i guess i have to forward the contact id in a different way:

            Intent intent = new Intent(Intent.ACTION_SEND);
            Uri uri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_URI, String.valueOf(con.id));
            intent.setData(uri);
            startActivity(intent);

Upvotes: 4

Views: 2048

Answers (1)

CommonsWare
CommonsWare

Reputation: 1006539

how can i share a Android contact using an intent?

It is unlikely that you can. A contact is a series of database records, and as such cannot usually be shared via ACTION_SEND.

Moreover, you do not use setData() with ACTION_SEND. Usually, you set EXTRA_TEXT or EXTRA_STREAM to be the text or Uri to share, and set the MIME type of the Intent to be the MIME type of the content in EXTRA_TEXT (text/plain) or EXTRA_STREAM. You may wish to review the documentation on ACTION_SEND for other alternatives.

My guess is that few apps will agree to share a contact, and those that do will crash when they try, as the Uri in EXTRA_STREAM is supposed to represent a stream (e.g., a file served by FileProvider), not a database-style entry.

You might consider generating some text or HTML that represents the contact, then sharing it, as that is more likely to be successful (e.g., to send in an email). Or, you could try standard contact encoding formats, like vCard, and see if that works.

Upvotes: 1

Related Questions