Android Question Insert a new contact with a QRCode VCard

zed

Active Member
Licensed User
Old thread

Hi all

There you go, everything is working fine.
Finally, it is quite simple to implement.
1 - Format the data
2 - Save a file in .vcf format
3 - create Intent.

Here is a complete example of the TAGs to fill in.
If no info to put, leave blank or simply do not put this TAG.
It is not mandatory to put all the TAGs.

BEGIN:VCARD
VERSION:3.0
FN;CHARSET=UTF-8:Name MiddleName SurName
N;CHARSET=UTF-8:Name;SurName;MiddleName;Prefix;Suffix
NICKNAME;CHARSET=UTF-8:NickName
UID;CHARSET=UTF-8:UID
BDAY:19700101
ANNIVERSARY:19710202
EMAIL;CHARSET=UTF-8;type=HOME,INTERNET:homeEmail
EMAIL;CHARSET=UTF-8;type=WORK,INTERNET:WorkEmail
TEL;TYPE=CELL:mobilePhone
TEL;TYPE=PAGER:yourPager_Phone
TEL;TYPE=HOME,VOICE:HomePhone
TEL;TYPE=WORK,VOICE:WorkPhone
TEL;TYPE=HOME,FAX:HomeFax
TEL;TYPE=WORK,FAX:WorkFax
LABEL;CHARSET=UTF-8;TYPE=HOME:homeLabel
ADR;CHARSET=UTF-8;TYPE=HOME:;;street;city;State/Province;PostalCode;country
LABEL;CHARSET=UTF-8;TYPE=WORK:workLabel
ADR;CHARSET=UTF-8;TYPE=WORK:;;wStreet;wCity;wState;wPostCode;wCountry
TITLE;CHARSET=UTF-8:wTitle
ROLE;CHARSET=UTF-8:wRole
ORG;CHARSET=UTF-8:company
URL;CHARSET=UTF-8:homeUrl&
URL;type=WORK;CHARSET=UTF-8:wUrl
NOTE;CHARSET=UTF-8:Note
REV:2023-05-09T19:31:12.155Z
End:VCARD

Here is the code
B4A:
' Format VCard
Dim s As String = $"BEGIN:VCARD
VERSION:3.0
FN;CHARSET=UTF-8:${Name} ${SurName}
N;CHARSET=UTF-8:${Name};${SurName};;;
EMAIL;CHARSET=UTF-8;type=HOME,INTERNET:${mail}
TEL;TYPE=CELL:${mobile}
ADR;CHARSET=UTF-8;TYPE=HOME:;;${street};${city};;${postcode};${country}
ORG;CHARSET=UTF-8:${company}
URL;CHARSET=UTF-8:${url}
End:VCARD
"$
' Remove tab
s = s.Replace(Chr(9),"")
Log(s)

' Creation of the QRCode and display
qr.Initialize(imvQR.Width)
imvQR.Bitmap = qr.Create(s)
    
' Save qrcode picture
Dim b As B4XBitmap = imvQR.Bitmap
Dim Out As OutputStream = File.OpenOutput(Path, FileName, False)
b.WriteToStream(Out, 100, "PNG")
Out.Close
        
' Creat vcf file
File.WriteString(Path, "vcard.vcf", s)
    
' copy to SharedFolder
Dim fp As FileProvider
fp.Initialize
File.Copy(Path, "vcard.vcf", fp.SharedFolder, "vcard.vcf")
    
'Insert in contacts
Dim thisintent As Intent
thisintent.Initialize(thisintent.ACTION_VIEW,fp.GetFileUri("vcard.vcf"))
thisintent.SetType("text/x-vcard")
thisintent.Flags = 0x01
StartActivity(thisintent)

with the kind support of @drgottjr. THANKS
 
Top