我想使用Android Api以VCard格式读取Android设备联系人。 我发现一个链接是一样的: Android contatcs vcard API如何使用android本地API以Vcard格式获取android联系人
,并试图写相同的代码,但它不工作,因为我没能拿到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);
谁能告诉我如何要获得上述代码的lookupkey,或者有任何其他方式,我们可以使用Android API获取联系人VCard格式。
我正在寻找如何在Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_VCARD_URI,lookupKey)得到LOOKUPKEY; –
你是对的我正在使用cur.getString(cur.getColumnIndex(Contacts.LOOKUP_KEY))请求查找键;如上所述,但它给出的例外:android.database.CursorIndexOutOfBoundsException:请求索引-1,大小为2 ,请在启动ContactComtract代码时出现错误,请解释。 –
你是否也看过http://code.google.com/p/vcardio/ –