Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 使用 vCard 檔案的方法簡單實例

編輯:關於 Android 程式設計

本文以實例講解 Android 如何使用 vCard 檔案,分享給大家參考。

 FileOutputStream os = null; try { os = VCardTest.this.openFileOutput("Android.vcf", MODE_PRIVATE); } catch (FileNotFoundException e1) { e1.printStackTrace(); } OutputStreamWriter writer; try { writer = new OutputStreamWriter(os); VCardComposer composer = new VCardComposer(); //create a contact ContactStruct contact1 = new ContactStruct(); contact1.name = "Neo"; contact1.company = "The Company"; contact1.addPhone(Contacts.Phones.TYPE_MOBILE, "+123456789", 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(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (VCardException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } 

上述程式碼的運作流程為:先透過 openFileOutput 建立一個 vcf 檔案的輸出串流,接著建立 VCardComposer 物件,並組裝 ContactStruct 聯絡人資料(包含姓名、公司與電話);然後呼叫 createVCard 產生 vCard 3.0 格式的字串,最後寫入輸出串流。若有多筆聯絡人,只需重複這個流程,並在每筆之間寫入空行作為分隔即可。

vCard 是一種通用的電子名片格式,廣泛應用於聯絡人的匯入與匯出,掌握它對開發通訊錄相關功能相當實用。

希望本文所述對大家 Android 程式設計有所幫助。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved