Export the Contacts as VCF file

前端 未结 8 674
梦毁少年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条回答
  •  -上瘾入骨i
    2020-12-01 03:42

    private void convertToVcfFile(String contactId, File contactFile) {
        Cursor mCursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                null, ContactsContract.CommonDataKinds.Phone._ID + " = " + contactId,
                null, null);
        if(mCursor != null && mCursor.moveToFirst()) {
            do {
                String mLookupKey = mCursor.getString(mCursor.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY));
                Uri mUri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_VCARD_URI, mLookupKey);
                try {
                    AssetFileDescriptor mAssetFileDescriptor = getContentResolver().openAssetFileDescriptor(mUri, "r");
                    if (mAssetFileDescriptor != null) {
                        FileInputStream mFileInputStream = mAssetFileDescriptor.createInputStream();
                        byte[] mBuffer = new byte[(int) mAssetFileDescriptor.getDeclaredLength()];
                        mFileInputStream.read(mBuffer);
                        String VCardString = new String(mBuffer);
                        FileOutputStream mFileOutputStream = new FileOutputStream(contactFile, true);
                        mFileOutputStream.write(VCardString.getBytes());
                    }
                } catch (Exception e1) {
                    e1.printStackTrace();
                }
            } while (mCursor.moveToNext());
            mCursor.close();
        }
    }
    

提交回复
热议问题