2011-11-07 47 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()); 

}