使用Cardme Java创建VCard

时间:2011-04-29 03:44:02

标签: java vcard

您好 我正在尝试使用Cardme API在Java中创建vcard(.vcf)文件。 我可以保存.vcf文件,但它没有内容并且为空。 请在下面找到我的代码,

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

感谢您解决此问题的任何帮助。

1 个答案:

答案 0 :(得分:3)

您需要导入正确的输出类:

import info.ineighborhood.cardme.io.VCardWriter;

或者在图书馆的最新版本(v0.3.3)中,包裹是:

import net.sourceforge.cardme.io.VCardWriter; 

然后使用它:

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