我正在尝试使用 Android Api 以 VCard 格式读取 Android 设备联系人。我找到了一个相同的链接: Android contatcs vcard API
并尝试编写相同的代码但它不起作用,因为我无法获得查找键:
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);
谁能告诉我如何获取上述代码的查找键,或者有任何其他方式我们可以使用 Android api 获取联系人 VCard 格式。