如何使用Android原生API来获取vCard格式的Android通讯录 [英] How to get android contacts in Vcard format using android native API
问题描述
我想读采用Android API中的vCard格式的Android设备的接触。
我发现一个链接是一样的:
的Android contatcs名片API
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
和尝试写同样的code,但它不工作,因为我无法获得lookupkey:
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);
谁能告诉我如何获得上述code中的lookupkey或有任何其他的方式,我们可以使用Android API获取联系人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.
推荐答案
- 编辑2 ---
-- Edit 2 ---
看起来你是不是读光标之前做cur.moveToFirst(),所以你得到exceptiion。尝试看看<一个href=\"http://stackoverflow.com/questions/4481895/android-database-cursorindexoutofboundsexception-index-1-requested\">android.database.CursorIndexOutOfBoundsException:索引-1请求的描述相同的问题。
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 在code是用于具体的联系。您正在使用的孔德例子是获得电子名片特定联系人。你必须循环,虽然现有的联系人和里面你可以把code,你必须得到它的工作。你可以从联系人合约查找键。
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:
- 请添加您在遇到错误
的logcat
- 有你的应用程序清单,允许应用程序读取联系人? 添加的联系人权限
- Please add error that you got in
logcat
- have you added contact permission in application manifest to allow application to read contacts?
这篇关于如何使用Android原生API来获取vCard格式的Android通讯录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!