Question

Hi I am trying to create a vcard (.vcf) file in Java using Cardme API. I can save a .vcf file, but it has no contents in it and empty. Please find my code below,

private void generateVCard(Card card){
    HelperClass helper = new HelperClass();
    VCardImpl vcard = new VCardImpl();
    BeginFeature begin = new BeginFeatureImpl();
    vcard.setBegin(begin);
    vcard.addEmail(helper.formEmailFeature(card));
    vcard.addAddress(helper.formAddress(card));
    vcard.addPhoto(helper.formPhotoFeature(card));
    vcard.addTelephoneNumber(helper.formTelephoneFeature(card));
    vcard.setName(helper.formNameFeature(card));
    vcard.setFormattedName(helper.formattedName(card));
    saveToFile("vc.vcf",vcard);
}
/**
   *  This function saves a VCard to disk.
   */
  public void saveToFile( String fileName , VCard vcard) {
      Writer output = null;   
      File file = new File("fileName");   
      try {
        output = new BufferedWriter(new FileWriter(file));
         output.write(vcard.toString());  
          output.flush();  
          output.close(); 
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }   
  }

Appreciate any help in resolving this issue.

Was it helpful?

Solution

You need to import the proper output class:

import info.ineighborhood.cardme.io.VCardWriter;

Or in latest versión of the library (v0.3.3) the package is:

import net.sourceforge.cardme.io.VCardWriter; 

and then use it:

/**
     *  This function saves a VCard to disk.
     */
    public static void saveToFile( String fileName , VCard vcard) {
    Writer output = null;   
    File file = new File("fileName");   
    try {
        output = new BufferedWriter(new FileWriter(file));
        VCardWriter writer = new VCardWriter();
        writer.setVCard(vcard);
        output.write(writer.buildVCardString());  
        output.flush();  
        output.close(); 
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }   
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top