Search code examples
androidqr-codezxingvcf-vcard

How to save vcard from qr code to contacts


I need to save to contacts if the qr code contains v card. I was trying to do that with this library but I could not read the v card information, I don't know which method is to use for that. I have seen a lot other similar posts but unfortunately those couldn't save me. I am using zxing library to read qr codes.


Solution

  • vCard is actually a bit verbose. You can use the MECARD format instead.

    Here you can see an example string from the ZXing Wiki:

    MECARD:N:Owen,Sean;ADR:76 9th Avenue, 4th Floor, New York, NY 10011;TEL:12125551212;EMAIL:srowen@example.com;;
    

    Now for the vCard format, I can see there's a class named VCardResultParser in the ZXing javadoc, and apparently it supports vCard v2.1. I made this small example stripping down some fields from the example in the wikipedia page for vCard:

    BEGIN:VCARD
    VERSION:2.1
    N:Gump;Forrest
    ORG:Bubba Gump Shrimp Co.
    TEL;WORK;VOICE:555
    ADR;HOME:;;42 Plantation St.;Baytown;LA;30314;USA
    EMAIL;PREF;INTERNET:forrestgump@example.com
    END:VCARD
    

    In both cases, you just have to serialize the fields to a plain text string and then you encode it onto a QRCode as usual. I tested both examples with a Samsung phone and when you scan them with ZXing, the leftmost button allows you to add a new contact. The contacts app opens, you press the "+" button and a new contact is created with the provided fields filled up.