简体   繁体   中英

How do I export contacts to vcard?

Is there any generic way to export contact information to a vcard 3.0 file in android? I'm currently working on a school project, where I want to upload all local contacts form android and ios to a web service in order to make the transition easier from android to android, android to ios and vice versa.

However, unfortunately in Android, it is rather difficult to get all the contact information at once, therefore, I would like to have a generic approach (eg some contacts do not have a profile picture, whereas others do).

I have come across some variants to solve this problem, however, I do not like them.

This is my current code to retrieve my contacts:

public  void getAndroidContacts(){

    // get contacts
    Cursor cursor_android_contacts = null;
    ContentResolver contentResolver = getContentResolver();

    try {
        cursor_android_contacts = contentResolver.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
    }catch (Exception e) {
        Log.e("Error on contact", e.getMessage());
    }

    // check hasContacts
    if (cursor_android_contacts.getCount()>0){
        while (cursor_android_contacts.moveToNext()){
            ContactSerializer.Android_Contact android_contact = new ContactSerializer.Android_Contact();

            // Get Name & Contact_ID
            String display_name=cursor_android_contacts.getString(cursor_android_contacts.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
            String contact_ID = cursor_android_contacts.getString(cursor_android_contacts.getColumnIndex(ContactsContract.CommonDataKinds.Phone._ID));

            android_contact.android_contact_ID = Integer.parseInt(contact_ID);
            android_contact.android_contact_name = display_name;

            // get Number
            int hasPhoneNumber = Integer.parseInt(cursor_android_contacts.getString(cursor_android_contacts.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER)));
            if (hasPhoneNumber > 0){
                Cursor phoneCursor = contentResolver.query(
                        ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                        null,
                        ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?",
                        new String[]{contact_ID},
                        null);

                while (phoneCursor.moveToNext()){
                    String phoneNumber = phoneCursor.getString(phoneCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));

                    // set number
                    android_contact.android_contact_number = phoneNumber;
                }
                phoneCursor.close();
            }
            ContactSerializer.localContacts.add(android_contact);
        }
        localContactCount = cursor_android_contacts.getCount();
    }// hasContacts
}

With this code I only get the id, name, and number; I would also like to know if the contact has a picture and if it is a private or business number.

Thanks for the help!

I managed to do it myself, however, this only gives you typically vCard V2.1:

private void getVcardString() {
    // TODO: 07.11.2017 Fix vCard Version
    vCardArray = new ArrayList<>();
    cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
            null, null, null);
    if (cursor != null && cursor.getCount() > 0) {
        cursor.moveToFirst();
        for (int i = 0; i < cursor.getCount(); i++) {

            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");
                FileInputStream fis = fd.createInputStream();
                byte[] buf = new byte[(int) fd.getDeclaredLength()];
                fis.read(buf);
                String vCard = new String(buf);
                vCardArray.add(vCard);
                Log.println(Log.ASSERT, "Vcard", vCard);
            } catch (Exception e1) {
                e1.printStackTrace();
            }
            // you could save the file here using a FileOutputStream
            cursor.moveToNext();
        }

    } else {
        Log.d("vCardERROR", "No Contacts in Your Phone");
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM