Extract contact list in vcf format

前端 未结 4 801
渐次进展
渐次进展 2020-12-09 23:46

How could i run the android method that extract the contact list in a vcf format?
Is there an intent that can directly call this action?

thx François

4条回答
  •  星月不相逢
    2020-12-10 00:51

    Here's how to extract to VCF file and share it via an intent, with the option of sharing a single contact if you wish:

        @WorkerThread
        @Nullable
        public static Intent getContactShareIntent(@NonNull Context context, @Nullable String contactId, @NonNull String filePath) {
            final ContentResolver contentResolver = context.getContentResolver();
            Cursor cursor = TextUtils.isEmpty(contactId) ? contentResolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null) :
                    contentResolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, Data.CONTACT_ID + "=?", new String[]{contactId}, null);
            if (cursor == null || !cursor.moveToFirst())
                return null;
            final File file = new File(filePath);
            file.delete();
            do {
                String lookupKey = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY));
                Uri uri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_VCARD_URI, lookupKey);
                AssetFileDescriptor fd;
                try {
                    fd = contentResolver.openAssetFileDescriptor(uri, "r");
                    if (fd == null)
                        return null;
                    FileInputStream fis = fd.createInputStream();
                    IOUtils.copy(fis, new FileOutputStream(filePath, false));
                    cursor.moveToNext();
                } catch (Exception e1) {
                    e1.printStackTrace();
                }
            } while (cursor.moveToNext());
            Intent intent = new Intent(Intent.ACTION_SEND);
            intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
            intent.setType("*/*");
            return intent;
        }
    

提交回复
热议问题