簡體   English   中英

如何為單個聯系人創建.vcf文件?

[英]How can I create .vcf file for a single contact?

我已經為Android設備中的所有聯系人成功創建了.vcf文件。 我已經提到了以下鏈接:

將聯系人導出為VCF文件

並且它的效果還不錯。 但是,我需要根據我的要求將單個聯系人轉換為單個.vcf文件。

在當前代碼中,使用以下代碼行獲取所有聯系人:

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

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

那么,我可以為單個聯系人生成.vcf文件的方法是什么?

請參見下面的代碼塊的第一行。 在這里我每次都會創建一個新的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();
        }
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM