Question

I am trying to read Android device contacts in VCard format using Android Api. i found one link for the same: Android contatcs vcard API

and trying to write the same code but its not working, as I am not able to get the lookupkey:

ContentResolver cr = getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,
                null, null, null, null);  
int num = cur.getCount();  // I get 2 , as there are two contacts

String lookupKey = cur.getString(cur.getColumnIndex(Contacts.LOOKUP_KEY));
// The above line gives error : android.database.CursorIndexOutOfBoundsException:
//  Index -1 requested, with a size of 2

Uri uri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_VCARD_URI, lookupKey);
AssetFileDescriptor fd = resolver.openAssetFileDescriptor(uri, "r");
FileInputStream fis = fd.createInputStream();
    byte[] b = new byte[(int)fd.getDeclaredLength()];
fis.read(b);
String vCard = new String(b);
sb.append(vCard);

Can anyone please tell me how to get the lookupkey for the above code or there is any other way we can get contacts VCard format using Android api.

Was it helpful?

Solution

-- Edit 2 ---

It looks like you are not doing cur.moveToFirst() before reading cursor, so you are getting exceptiion. try look into android.database.CursorIndexOutOfBoundsException: Index -1 requested which describes the same problem.

-- Edited answer --

LookUpKey in the code is for a specific contact. The conde example you are using is to get the vcard for a specific contact. you have to loop though the available contacts and inside you can put the code you have to get it working. You can get look up key from contact contract.

apart from that, you should consider following a general solution:

  1. Please add error that you got in logcat
  2. have you added contact permission in application manifest to allow application to read contacts?

OTHER TIPS

Here you are:

Cursor cursor = context.getContentResolver().query(
                ContactsContract.Contacts.CONTENT_URI, null, null, null, null);

if (cursor != null && cursor.moveToFirst()) {
    try {
        do {
            String lookupKey = cursor.getString(cursor
                    .getColumnIndex(Contacts.LOOKUP_KEY));


            Uri uri = Uri.withAppendedPath(
                    ContactsContract.Contacts.CONTENT_VCARD_URI,
                    lookupKey);
            AssetFileDescriptor fd;
            try {
                fd = context.getContentResolver()
                        .openAssetFileDescriptor(uri, "r");
                FileInputStream fis = fd.createInputStream();
                byte[] b = new byte[(int) fd.getDeclaredLength()];
                fis.read(b);
                String vCard = new String(b);
                Log.i(TAG, vCard);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        } while (cursor.moveToNext());
    } finally {
        cursor.close();
    }
}
public static void getVCF() {
    final String vfile = "Contacts.csv";
    Cursor phones = mContext.getContentResolver().query(
            ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null,
            null, null);
    phones.moveToFirst();

    do {
        String lookupKey = phones.getString(phones
                .getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY));

        Uri uri = Uri.withAppendedPath(
                ContactsContract.Contacts.CONTENT_VCARD_URI, lookupKey);
        AssetFileDescriptor fd;
        try {
            fd = mContext.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,
                    true);
            mFileOutputStream.write(VCard.toString().getBytes());
            phones.moveToNext();
            Log.d("Vcard", VCard);
        } catch (Exception e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
    } while (phones.moveToNext());

}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top