如何使用 Android 原生 API 获取 Vcard 格式的 Android 联系人
我正在尝试使用 Android Api 读取 VCard 格式的 Android 设备联系人。 我找到了一个相同的链接: Android contatcs vcard API
并尝试编写相同的代码,但它不起作用,因为我没有能够获取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格式。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
给你:
Here you are:
-- 编辑2 ---
看起来您在读取光标之前没有执行 cur.moveToFirst() ,所以您遇到了异常。尝试查看android.database.CursorIndexOutOfBoundsException:索引-1请求,它描述了同样的问题。
-- 编辑后的答案 --
代码中的 LookUpKey 用于特定的联系方式。您使用的 conde 示例是获取特定联系人的 vcard。您必须循环访问可用的联系人,并且可以在其中放置使其工作所需的代码。您可以从联系合同中获取查找密钥。
除此之外,您应该考虑遵循通用解决方案:
logcat
中遇到的错误-- 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:
logcat