2012-03-07 57 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

相关问题