如何使用 Android 原生 API 获取 Vcard 格式的 Android 联系人

发布于 2024-12-14 05:23:42 字数 1058 浏览 1 评论 0原文

我正在尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

你げ笑在眉眼 2024-12-21 05:23:42

给你:

Cursor cursor = context.getContentResolver().query(
                ContactsContract.Contacts.CONTENT_URI, null, null, null, null);

if (cursor != null && cursor.moveToFirst()) {
    try {
        do {
            String lookupKey = cursor.getString(cursor
                    .getColumnIndex(Contacts.LOOKUP_KEY));


            Uri uri = Uri.withAppendedPath(
                    ContactsContract.Contacts.CONTENT_VCARD_URI,
                    lookupKey);
            AssetFileDescriptor fd;
            try {
                fd = context.getContentResolver()
                        .openAssetFileDescriptor(uri, "r");
                FileInputStream fis = fd.createInputStream();
                byte[] b = new byte[(int) fd.getDeclaredLength()];
                fis.read(b);
                String vCard = new String(b);
                Log.i(TAG, vCard);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        } while (cursor.moveToNext());
    } finally {
        cursor.close();
    }
}

Here you are:

Cursor cursor = context.getContentResolver().query(
                ContactsContract.Contacts.CONTENT_URI, null, null, null, null);

if (cursor != null && cursor.moveToFirst()) {
    try {
        do {
            String lookupKey = cursor.getString(cursor
                    .getColumnIndex(Contacts.LOOKUP_KEY));


            Uri uri = Uri.withAppendedPath(
                    ContactsContract.Contacts.CONTENT_VCARD_URI,
                    lookupKey);
            AssetFileDescriptor fd;
            try {
                fd = context.getContentResolver()
                        .openAssetFileDescriptor(uri, "r");
                FileInputStream fis = fd.createInputStream();
                byte[] b = new byte[(int) fd.getDeclaredLength()];
                fis.read(b);
                String vCard = new String(b);
                Log.i(TAG, vCard);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        } while (cursor.moveToNext());
    } finally {
        cursor.close();
    }
}
黎夕旧梦 2024-12-21 05:23:42

-- 编辑2 ---

看起来您在读取光标之前没有执行 cur.moveToFirst() ,所以您遇到了异常。尝试查看android.database.CursorIndexOutOfBoundsException:索引-1请求,它描述了同样的问题。

-- 编辑后的答案 --

代码中的 LookUpKey 用于特定的联系方式。您使用的 conde 示例是获取特定联系人的 vcard。您必须循环访问可用的联系人,并且可以在其中放置使其工作所需的代码。您可以从联系合同中获取查找密钥。

除此之外,您应该考虑遵循通用解决方案:

  1. 请添加您在 logcat 中遇到的错误
  2. 您是否在应用程序清单中添加了联系人权限以允许应用程序读取联系人?

-- 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:

  1. Please add error that you got in logcat
  2. have you added contact permission in application manifest to allow application to read contacts?
呢古 2024-12-21 05:23:42
public static void getVCF() {
    final String vfile = "Contacts.csv";
    Cursor phones = mContext.getContentResolver().query(
            ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null,
            null, null);
    phones.moveToFirst();

    do {
        String lookupKey = phones.getString(phones
                .getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY));

        Uri uri = Uri.withAppendedPath(
                ContactsContract.Contacts.CONTENT_VCARD_URI, lookupKey);
        AssetFileDescriptor fd;
        try {
            fd = mContext.getContentResolver().openAssetFileDescriptor(uri,
                    "r");
            FileInputStream fis = fd.createInputStream();
            byte[] buf = new byte[(int) fd.getDeclaredLength()];
            fis.read(buf);
            String VCard = new String(buf);
            String path = Environment.getExternalStorageDirectory()
                    .toString() + File.separator + vfile;
            FileOutputStream mFileOutputStream = new FileOutputStream(path,
                    true);
            mFileOutputStream.write(VCard.toString().getBytes());
            phones.moveToNext();
            Log.d("Vcard", VCard);
        } catch (Exception e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
    } while (phones.moveToNext());

}
public static void getVCF() {
    final String vfile = "Contacts.csv";
    Cursor phones = mContext.getContentResolver().query(
            ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null,
            null, null);
    phones.moveToFirst();

    do {
        String lookupKey = phones.getString(phones
                .getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY));

        Uri uri = Uri.withAppendedPath(
                ContactsContract.Contacts.CONTENT_VCARD_URI, lookupKey);
        AssetFileDescriptor fd;
        try {
            fd = mContext.getContentResolver().openAssetFileDescriptor(uri,
                    "r");
            FileInputStream fis = fd.createInputStream();
            byte[] buf = new byte[(int) fd.getDeclaredLength()];
            fis.read(buf);
            String VCard = new String(buf);
            String path = Environment.getExternalStorageDirectory()
                    .toString() + File.separator + vfile;
            FileOutputStream mFileOutputStream = new FileOutputStream(path,
                    true);
            mFileOutputStream.write(VCard.toString().getBytes());
            phones.moveToNext();
            Log.d("Vcard", VCard);
        } catch (Exception e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
    } while (phones.moveToNext());

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文