2012-09-01 160 views
5

我試圖從聯繫人選擇器中檢索聯繫人圖片,聯繫人姓名和手機號碼。一旦我選擇了聯繫人選擇器中的聯繫人,我希望能夠檢索這三件事情。android獲取聯繫人圖片,姓名和手機號碼

我已經成功地從聯繫人選擇器中檢索聯繫人ID,但似乎無法使用該ID獲取其他任何內容。

我已經看過各種示例代碼,它們都使用遊標,並用光標做不同的事情,但我不明白它是如何工作的。有人可以幫我嗎?

下面是一些代碼,我試過:

public void launchContactPicker(View view) { 
    Intent contactPickerIntent = new Intent(Intent.ACTION_PICK, 
      ContactsContract.Contacts.CONTENT_URI); 
    startActivityForResult(contactPickerIntent, CONTACT_PICKER_RESULT); 
} 

protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    if (resultCode == RESULT_OK) { 
     switch (requestCode) { 
      case CONTACT_PICKER_RESULT: 
       Cursor cursor; 
       Bundle extras = data.getExtras(); 
       if(extras != null) 
       { 
        Set<String> keys = extras.keySet(); 
        for (String key : keys) { 
         extras.get(key); 
        } 
       } 
       else 
       { 
        Log.v("Auto Respond", "No extras returned from contact."); 
       } 

       Uri result = data.getData(); 
       String id = result.getLastPathSegment(); 

       cursor = getContentResolver().query(
         ContactsContract.CommonDataKinds.Phone.CONTENT_URI, 
         null, 
         ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?", 
         new String[]{id}, null); 

       contactName = cursor.getString(cursor.getColumnIndex((ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME))); 

       Uri photo = Uri.withAppendedPath(result, ContactsContract.Contacts.Photo.CONTENT_DIRECTORY); 
       //contactPicture = Contacts.People.loadContactPhoto(this, getStuff, R.drawable.ic_contact_picture, null); 
       contactNumber = cursor.getString(cursor.getColumnIndex((ContactsContract.CommonDataKinds.Phone.NUMBER))); 

       //Log.v(DEBUG_TAG, "Got a result: " + result.toString()); 
       cursor.close(); 
       break; 
     } 

    } else { 
     // gracefully handle failure 
     Log.w("Auto Respond", "Warning: activity result not ok"); 
    } 
} 

直到光標被賦予它的工作原理了。我正確地獲得ID,但是當我嘗試檢索姓名,號碼或聯繫人圖片時,我獲得了一個FC。

這是我從logcat中獲取:

E/AndroidRuntime(23957): FATAL EXCEPTION: main 
E/AndroidRuntime(23957): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1001, result=-1, data=Intent { dat=content://com.android.contacts/contacts/lookup/1760i2bd7c02a0fcd76ea/407 flg=0x1 }} to activity {com.havens1515.autorespond/com.havens1515.autorespond.NoResponse}: android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 1 
E/AndroidRuntime(23957): at android.app.ActivityThread.deliverResults(ActivityThread.java:3264) 
E/AndroidRuntime(23957): at android.app.ActivityThread.handleSendResult(ActivityThread.java:3307) 
E/AndroidRuntime(23957): at android.app.ActivityThread.access$1100(ActivityThread.java:139) 
E/AndroidRuntime(23957): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1253) 
E/AndroidRuntime(23957): at android.os.Handler.dispatchMessage(Handler.java:99) 
E/AndroidRuntime(23957): at android.os.Looper.loop(Looper.java:137) 
E/AndroidRuntime(23957): at android.app.ActivityThread.main(ActivityThread.java:4896) 
E/AndroidRuntime(23957): at java.lang.reflect.Method.invokeNative(Native Method) 
E/AndroidRuntime(23957): at java.lang.reflect.Method.invoke(Method.java:511) 
E/AndroidRuntime(23957): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791) 
E/AndroidRuntime(23957): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:558) 
E/AndroidRuntime(23957): at dalvik.system.NativeStart.main(Native Method) 
E/AndroidRuntime(23957): Caused by: android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 1 
E/AndroidRuntime(23957): at android.database.AbstractCursor.checkPosition(AbstractCursor.java:418) 
E/AndroidRuntime(23957): at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136) 
E/AndroidRuntime(23957): at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:50) 
E/AndroidRuntime(23957): at android.database.CursorWrapper.getString(CursorWrapper.java:114) 
E/AndroidRuntime(23957): at com.havens1515.autorespond.NoResponse.onActivityResult(NoResponse.java:206) 
E/AndroidRuntime(23957): at android.app.Activity.dispatchActivityResult(Activity.java:5192) 
E/AndroidRuntime(23957): at android.app.ActivityThread.deliverResults(ActivityThread.java:3260) 
E/AndroidRuntime(23957): ... 11 more 
+0

您是否嘗試過任何代碼? – Swayam

+0

編輯帖子,添加了一些我試過的代碼。 – Randy

+0

啊太棒了!現在你說有一個FC,我們可以看看logcat嗎? – Swayam

回答

1

你的光標試圖訪問-1指數,因此錯誤。嘗試使用cursor.moveToFirst(),然後嘗試從列中獲取詳細信息。

相關問題