如何在Android下以编程方式从.vcf文件导入或插入联系人?

Pra*_*dam 3 android vcf android-contacts

.vcf使用下面的代码创建Android中所有联系人的文件.

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();
         }
    }
}
Run Code Online (Sandbox Code Playgroud)

如何.vcf在Android中以编程方式从文件添加/恢复联系人?

我想.vcf使用代码将该文件中的所有联系人恢复到Android.这该怎么做?

pyu*_*s13 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);
Run Code Online (Sandbox Code Playgroud)

请享用!!

  • +1 对于工作内容,但认为这不能回答问题。我认为问题是指如何以编程方式进行操作,而不是使用处理 vcard 的外部应用程序。 (2认同)