如何为单个联系人创建.vcf文件?

时间:2018-09-19 04:35:53

标签: android android-contacts vcf

我已经为Android设备中的所有联系人成功创建了.vcf文件。我已经在下面提到了这样做的链接:

Export the Contacts as VCF file

,它的效果还不错。 但是,我需要根据我的要求将单个联系人转换为单个.vcf文件。

在当前代码中,使用以下几行获取所有联系人:

cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);

,然后所有联系人都将包含在.vcf文件中。

那么,我可以为单个联系人生成.vcf文件的方式是什么?

1 个答案:

答案 0 :(得分:1)

请参见下面的代码块的第一行。在这里,我每次都会创建一个新的 vFile ,因此将调用此方法。因此,每个联系人都将保存在不同的文件中。

public void get(Cursor cursor)
    {
        vfile = "Contacts" + "_" + System.currentTimeMillis()+".vcf";


        //cursor.moveToFirst();
        String lookupKey = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY));
        Uri uri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_VCARD_URI, lookupKey);
        AssetFileDescriptor fd;
        try {
            fd = this.getContentResolver().openAssetFileDescriptor(uri, "r");

            // Your Complex Code and you used function without loop so how can you get all Contacts Vcard.??


           /* 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 out = new FileOutputStream(path);
            out.write(VCard.toString().getBytes());
            Log.d("Vcard",  VCard);*/

            FileInputStream fis = fd.createInputStream();
            byte[] buf = new byte[(int) fd.getDeclaredLength()];
            fis.read(buf);
            String vcardstring= new String(buf);
            vCard.add(vcardstring);

            String storage_path = Environment.getExternalStorageDirectory().toString() + File.separator + vfile;
            FileOutputStream mFileOutputStream = new FileOutputStream(storage_path, false);
            mFileOutputStream.write(vcardstring.toString().getBytes());

        } catch (Exception e1) 
        {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
    }