2012-03-07 59 views
3

我使用下面的代碼創建了Android中所有聯繫人的.vcf文件。如何以編程方式從Android中的.vcf文件導入或插入聯繫人?

public static void getVCF() 
{ 
    final String vfile = "POContactsRestore.vcf"; 
    Cursor phones = mContext.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, 
        null, null, null); 
    phones.moveToFirst(); 
    for(int i =0;i<phones.getCount();i++) 
    { 
     String lookupKey = phones.getString(phones.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY)); 
     Uri uri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_VCARD_URI, lookupKey); 
     AssetFileDescriptor fd; 
     try 
     { 
      fd = mContext.getContentResolver().openAssetFileDescriptor(uri, "r"); 
      FileInputStream fis = fd.createInputStream(); 
      byte[] buf = new byte[(int) fd.getDeclaredLength()]; 
      fis.read(buf); 
      String VCard = new String(buf); 
      String path = Environment.getExternalStorageDirectory().toString() + File.separator + vfile; 
      FileOutputStream mFileOutputStream = new FileOutputStream(path, true); 
         mFileOutputStream.write(VCard.toString().getBytes());   
      phones.moveToNext();       
      Log.d("Vcard", VCard); 
     } 
     catch (Exception e1) 
     { 
       // TODO Auto-generated catch block 
       e1.printStackTrace(); 
     } 
    } 
} 

如何在Android中以編程方式從.vcf文件添加/恢復聯繫人?

我想要使用代碼將所有聯繫人表單.vcf文件還原到Android。 如何做到這一點?

+0

它是使用完整的我的應用程序.... – NagarjunaReddy 2012-10-11 11:23:41

回答

9

其實我也想做同樣的事情,經過長時間的研究,終於找到了解決辦法。

下面是一段代碼來恢復:

Intent intent = new Intent(Intent.ACTION_VIEW); 
intent.setDataAndType(Uri.fromFile(new File(storage_path+vfile)),"text/x-vcard"); //storage path is path of your vcf file and vFile is name of that file. 
startActivity(intent); 

享受!

+1

+1的工作的東西,但認爲這並不回答這個問題。我認爲這個問題涉及如何以編程方式執行此操作,而不是使用處理vcard的外部應用程序。 – 2013-04-14 10:03:28

+0

另外,我認爲問題是如何在導入vcard時使用CONTENT_VCARD_URI - 而不僅僅是導出。 – 2013-04-14 10:05:09

+0

@ pyus13如何停止導入重複的聯繫人。有沒有我們可以通過的來實現目標。 – 2014-11-06 06:20:32

相關問題