Android vcard uses the sample to generate the VCF file

Source: Internet
Author: User
Tags create vcard vcard

Android vcard uses the sample to generate the VCF file

When we back up the phone contacts, when exporting to the SD card, a VCF file is generated in the SD card to save the contact name and phone number.

The VCard specification allows for public exchange of personal Data exchange (Personal Data Interchange PDI) information, which can be found in traditional paper business cards. The specification defines the format of an Electronic Business card (or vCard).

Using a vcard on Android requires a third-party package:

To copy it into the project and then add the jar, the implementation code is simple, as follows:

 if (Environment.getexternalstoragestate (). Equals (environment.media_mounted))//Determine if the memory card exists {OutputStreamWriter     Writer  File File = new file (Environment.getexternalstoragedirectory (), "example.vcf");//Gets the root path of the memory card, writes EXAMPLE.VCF to the root directory, try {             writer = new OutputStreamWriter (new FileOutputStream (file), "UTF-8");            Create a contact Vcardcomposer composer = new Vcardcomposer ();        Contactstruct Contact1 = new Contactstruct ();         Contact1.name = "John";        Contact1.company = "the Company";        Contact1.addphone (Contacts.Phones.TYPE_MOBILE, "15651865008", null, TRUE);     Create VCard representation String vcardstring;            vcardstring = Composer.createvcard (Contact1, vcardcomposer.version_vcard30_int);              Write VCard to the output stream Writer.write (vcardstring); Writer.write ("/n");     Add empty lines between contacts//Repeat for other contacts//... writer.close ();    Toast.maketext (c, "The SD card was successfully imported!) ", Toast.length_short). Show ();}  catch (Unsupportedencodingexception e) {//TODO auto-generated catch block E.printstacktrace ();  } catch (FileNotFoundException e) {//TODO auto-generated catch block E.printstacktrace ();  } catch (Vcardexception e) {//TODO auto-generated catch block E.printstacktrace ();  } catch (IOException e) {//TODO auto-generated catch block E.printstacktrace (); }} else{Toast.maketext (c, "Write failed, SD card does not exist!) ", Toast.length_short). Show ();}


To read and write to the memory card, read and Write permissions are added:

<uses-permission android:name= "Android.permission.WRITE_EXTERNAL_STORAGE" ></uses-permission>


This allows the contact to be backed up successfully, with the system's own contact software to implement the import. Here is just a simple example of writing data, reading the data in the VCF file example I have and a compression upload, for the students to download,: http://download.csdn.net/detail/pzhtpf/4564761

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.