我想使用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/ –