开发者

Create VCard using Cardme Java

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 funct开发者_如何学Goion 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.


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();
    }   
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜