Export the Contacts as VCF file

前端 未结 8 676
梦毁少年i
梦毁少年i 2020-12-01 03:16

I want to export the Phone contacts to External storage area. I didn\'t work with this type of method. Anyone guide me to do this?

8条回答
  •  一整个雨季
    2020-12-01 03:59

    I have removed the exception and other error and below is my CODE :

        private final String vfile = "POContactsRestore.vcf";
        Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
                        null, null, null);
                phones.moveToFirst();
                String lookupKey = phones.getString(phones.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY));
                Uri uri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_VCARD_URI, lookupKey);
                AssetFileDescriptor fd;
                try {
                    fd = this.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, false);
                    mFileOutputStream.write(vCard.toString().getBytes());
                    Log.d("Vcard",  vCard);
                } catch (Exception e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
    

    If you can iterate through loop and get the vCard for the contacts and store it in the SDCARD.

提交回复
热议问题