Android vcard使用樣本,產生vcf檔案

來源:互聯網
上載者:User

  我們備份手機連絡人時,匯出到SD卡時,會在SD卡中產生一個vcf檔案,用於儲存連絡人姓名,手機號碼。
vCard 規範容許公開交換個人資料交換(Personal Data Interchange PDI)資訊,在傳統紙質商業名片可找到這些資訊。規範定義電子名片(或叫vCard)的格式。
而在Android上使用vcard就要藉助第三方包:
 
 
將它複製進工程,然後Add jar即可,實現代碼很簡單,如下:
[html] 
 if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) //判斷儲存卡是否存在 
     { 
        OutputStreamWriter writer;  
        File file = new File(Environment.getExternalStorageDirectory(),"example.vcf"); 
//得到儲存卡的根路徑,將example.vcf寫入到根目錄下 
    try {   
        writer = new OutputStreamWriter(new FileOutputStream(file), "UTF-8"); 
           
           //create a contact    
               VCardComposer composer = new VCardComposer();   
               ContactStruct contact1 = new ContactStruct();  
           contact1.name ="John" ;   
           contact1.company = "The Company";  
           contact1.addPhone(Contacts.Phones.TYPE_MOBILE, "15651865008", 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();   
           Toast.makeText(c, "已成功匯入SD卡中!", Toast.LENGTH_SHORT).show(); 
    } catch (UnsupportedEncodingException e) {   
    // TODO Auto-generated catch block    
    e.printStackTrace();   
    } catch (FileNotFoundException e) {   
    // TODO Auto-generated catch block    
    e.printStackTrace();   
    } catch (VCardException e) {   
    // TODO Auto-generated catch block    
    e.printStackTrace();   
    } catch (IOException e) {   
    // TODO Auto-generated catch block    
    e.printStackTrace();   
    }   
 
} 
     else{ 
         Toast.makeText(c, "寫入失敗,SD卡不存在!", Toast.LENGTH_SHORT).show(); 
     } 
 
 
由於要對儲存卡做讀寫操作,所以要加讀寫權限:
[html] 
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission> 
 
這樣連絡人就備份成功了,用系統內建的連絡人軟體就可以實現匯入。這裡只是個簡單的寫資料的例子,讀取vcf檔案中的資料的例子我已經和一起壓縮上傳,

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.