2011-11-07 50 views
2

我想使用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格式。

回答

1

- 编辑2 ---

看起来你是不读的光标之前做cur.moveToFirst(),所以你得到exceptiion。请尝试查看描述相同问题的android.database.CursorIndexOutOfBoundsException: Index -1 requested

- 编辑答案 -

代码LookUpKey为特定的联系人。您正在使用的conde示例是为特定联系人获取vcard。你必须循环使用可用的联系人,并且在里面你可以把代码放到工作中。您可以从联系合同中查找密钥。

除此之外,你应该考虑以下的通用解决方案:

  1. 请添加您在logcat
  2. 有错误你的应用程序清单,允许应用程序读取联系人添加联系人权限?
+0

我正在寻找如何在Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_VCARD_URI,lookupKey)得到LOOKUPKEY; –

+0

你是对的我正在使用cur.getString(cur.getColumnIndex(Contacts.LOOKUP_KEY))请求查找键;如上所述,但它给出的例外:android.database.CursorIndexOutOfBoundsException:请求索引-1,大小为2 ,请在启动ContactComtract代码时出现错误,请解释。 –

+0

你是否也看过http://code.google.com/p/vcardio/ –

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(); 
    } 
} 
+0

我得到对于'fd = context.getContentResolver()'行来说'上下文无法解析'。 – Dennis

+0

将其更改为'fd = getContentResolver()'不会产生即时错误。 – Dennis

+0

此代码在Android 4.1中不可用 – Rohit

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

}