2011-04-29 65 views
3

嗨 我正嘗試使用Cardme API在Java中創建vcard(.vcf)文件。 我可以保存一個.vcf文件,但它沒有內容並且是空的。 請在下面找到我的代碼,使用Cardme創建VCard Java

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

欣賞解決此問題的任何幫助。

回答

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

public電話功能formTelephoneFeature(Card card){ \t \t TelephoneFeaturephoneFeature = new TelephoneFeatureImpl(); \t \t telephoneFeature.setTelephone(card.getContact()。getHmePhn()); \t \t return telephoneFeature; \t} – jshree 2011-04-30 20:47:47

+0

不確定究竟應該做什麼?上面的代碼塊將把VCard輸出保存到一個文件中。 – Femi 2011-04-30 20:56:05

+0

對不起,我完全沒有完成我最後的評論。我試過你的解決方案。當我在線程「main」中使用formTelephoneFeature()method.Exception時,出現以下錯誤:info.ineighborhood.cardme.vcard.errors.VCardBuildException:TelephoneFeature(TEL)[info.ineighborhood.cardme.vcard.errors.VCardBuildException ]電話功能(TEL)存在但是爲空。 – jshree 2011-04-30 21:02:56