2

我正在尝试使用 Android Api 以 VCard 格式读取 Android 设备联系人。我找到了一个相同的链接: Android contatcs vcard API

并尝试编写相同的代码但它不起作用,因为我无法获得查找键:

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);

谁能告诉我如何获取上述代码的查找键,或者有任何其他方式我们可以使用 Android api 获取联系人 VCard 格式。

4

3 回答 3

7

这个给你:

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();
    }
}
于 2012-03-08T10:17:47.467 回答
1

-- 编辑 2 ---

看起来你在读取光标之前没有做 cur.moveToFirst() ,所以你得到了异常。尝试查看描述相同问题的android.database.CursorIndexOutOfBoundsException: Index -1 requested 。

-- 修改后的答案 --

代码中的LookUpKey是针对特定联系人的。您使用的 conde 示例是获取特定联系人的电子名片。您必须遍历可用的联系人,并且可以在其中放置您必须使其工作的代码。您可以从联系合同中获取查找密钥。

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

  1. 请添加您输入的错误logcat
  2. 您是否在应用程序清单中添加了联系人权限以允许应用程序读取联系人?
于 2011-11-07T11:34:06.237 回答
0
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());

}
于 2014-04-03T05:39:04.883 回答