如何在不使用Intents的情况下从.vcf文件恢复android中的联系人

时间:2013-10-17 09:14:50

标签: android android-contentprovider android-contacts

stackoverflow我正在尝试开发一个可以backuprestore联系人的应用,backup没问题,我正在使用以下方法备份联系人作为VCF格式文件

public static void getVCF() {
    final String vfile = "contacts.vcf";

    Cursor phones = mContext.getContentResolver().query(
            ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null,
            null, ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC");
    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();
        }
    }
}

但是restore是个谜,我在互联网上搜索了很多但没有成功,我尝试backup使用Intent,但这不是问题的解决方案,代码如下

            final Intent intent = new Intent();

            final MimeTypeMap mime = MimeTypeMap.getSingleton();
            String tmptype = mime.getMimeTypeFromExtension("vcf");
            final File file = new File(Environment.getExternalStorageDirectory().toString() +"/contacts.vcf");

            intent.setDataAndType(Uri.fromFile(file),tmptype);
            startActivity(intent);

Permissions

<uses-permission android:name="android.permission.READ_CONTACTS"/>
<uses-permission android:name="android.permission.WRITE_CONTACTS"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

我希望restore使用Intents来正确联系{{1}}。我发现了这些信息丰富的帖子How to import contacts from VCF file by using code 但最后我对如何使用代码感到困惑,任何人都可以帮我解决这个难题。

感谢。

0 个答案:

没有答案