如何从Android中以编程方式从存储在SD卡中的VCF文件导入联系人

时间:2018-07-26 05:14:23

标签: android contacts vcf

我将vcf文件存储在Sdcard中,但现在我必须以编程方式将该vcf文件导入联系人中

我已通过以下代码创建了vcf文件

final String vfile = "Contacts.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();
        } catch (Exception e1) {
            e1.printStackTrace();
        }
    }
}

通过此contact.vcf在sdcard中创建 -现在我必须以编程方式在联系人中导入此vcf文件

并使用以下代码恢复了vcf文件

Intent intent = new Intent(Intent.ACTION_VIEW);

intent.setDataAndType(uri, "text/x-vcard");
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

startActivity(intent);

-但不使用此代码同步联系人

0 个答案:

没有答案