2

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?

General Grievance
  • 4,555
  • 31
  • 31
  • 45

3 Answers3

1

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;
    }
android developer
  • 114,585
  • 152
  • 739
  • 1,270
0

This has changed as of Sept., 2016. You can now export your contacts to a VCF file using the 'People' app: select 'Import/export' in the menu, then one of the export items. You'll still have to get the file off your phone but that's easy enough via ADB (or maybe email.)

blamblambunny
  • 757
  • 8
  • 19
0

You can do this manually in the contacts App. But there is no Intent for that. If you want your App to have such an export you unfortunately have to write it yourself or use code from here.vcardio

Thommy
  • 5,070
  • 2
  • 28
  • 51