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

问题描述 投票:0回答:1

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

Export the Contacts as VCF file

它的工作非常好。但是,我需要根据我的要求将单个联系人转换为单个.vcf文件。

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

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

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

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

android android-contacts vcf
1个回答
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();
        }
    }
© www.soinside.com 2019 - 2024. All rights reserved.