Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to Share a contact that doesn't exists? (only info) in Android?

Context: I have a QR scanner app that can scan contacts info. upon scanned, the user can add the contact or share it. how can I share (if I can) to another app?

I have this code right here to add contact intent:

fun createContactIntent(
    name: String,
    phones: Map<Int, String>,
    emails: Map<Int, String>,
    jobTitle: String,
    organization: String,
    addresses: List<String>,
    urls: List<String>
): Intent {
    val data = ArrayList<ContentValues>()
    val intent = Intent(ContactsContract.Intents.Insert.ACTION).apply {
        type = ContactsContract.Contacts.CONTENT_TYPE
        putExtra(ContactsContract.Intents.Insert.NAME, name)
        putExtra(ContactsContract.Intents.Insert.JOB_TITLE, jobTitle)
        putExtra(ContactsContract.Intents.Insert.COMPANY, organization)
    }
    addresses.forEach {
        val row = ContentValues()
        row. Put(ContactsContract.Data.MIMETYPE,         ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE)
        row. Put(ContactsContract.CommonDataKinds.SipAddress.SIP_ADDRESS, it)
        data. Add(row)
    }
    urls.forEach {
        val row = ContentValues()
        row. Put(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Website.CONTENT_ITEM_TYPE)
        row. Put(ContactsContract.CommonDataKinds.Website.URL, it)
        data. Add(row)
    }
    emails.forEach{ (type, address) ->
        val row = ContentValues()
        row. Put(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE)
        row. Put(ContactsContract.CommonDataKinds.Email.ADDRESS, address)
        row. Put(ContactsContract.CommonDataKinds.Email.TYPE, type)
        data. Add(row)
    }
    phones.forEach { (type, phone) ->
        val row = ContentValues()
        row. Put(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
        row. Put(ContactsContract.CommonDataKinds.Phone.NUMBER, phone)
        row. Put(ContactsContract.CommonDataKinds.Phone.TYPE, type)
        data. Add(row)
    }
    intent.putParcelableArrayListExtra(ContactsContract.Intents.Insert.DATA, data)

    return intent
}

And it works great, but how can i share the contact info via share intent?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

When it comes to sharing a contact’s information that doesn’t exist in the phone’s contact list, one way to achieve this is by sending a vCard (Virtual Contact File) using Android’s sharing intent. The vCard format (.vcf file) is a universally accepted format for sharing contacts.

The steps involved are:

  1. Create a vCard string from the contact information.
  2. Use Android’s Intent system to share this vCard string.

Here’s a brief solution to share a contact’s information using a vCard:

fun shareContact(
    name: String,
    phones: Map<Int, String>,
    emails: Map<Int, String>,
    jobTitle: String,
    organization: String,
    addresses: List<String>,
    urls: List<String>
) {
    // Create vCard
    val vCard = StringBuilder()
    vCard.append("BEGIN:VCARD\n")
    vCard.append("VERSION:3.0\n")
    vCard.append("FN:$name\n")
    vCard.append("ORG:$organization\n")
    vCard.append("TITLE:$jobTitle\n")
    phones.forEach { (_, phone) ->
        vCard.append("TEL:$phone\n")
    }
    emails.forEach { (_, email) ->
        vCard.append("EMAIL:$email\n")
    }
    addresses.forEach { address ->
        vCard.append("ADR:;;$address\n")
    }
    urls.forEach { url ->
        vCard.append("URL:$url\n")
    }
    vCard.append("END:VCARD\n")

    // Share vCard
    val shareIntent = Intent().apply {
        action = Intent.ACTION_SEND
        type = "text/x-vcard"
        putExtra(Intent.EXTRA_TEXT, vCard.toString())
    }

    // Start the share intent
    startActivity(Intent.createChooser(shareIntent, "Share contact using"))
}

Here, the shareContact function constructs a vCard string with the provided contact details and then uses a sharing intent to send this vCard to other apps. The receiving app can then interpret the vCard to get the contact details.

Note: This is a basic example. The vCard format can be more complex, supporting many different fields and properties. Make sure to test this across devices to ensure compatibility. Also, for better performance with larger data, you might consider using StringBuffer instead of StringBuilder.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading