5

我正在尝试从联系人选择器中检索联系人图片、联系人姓名和手机号码。在联系人选择器中选择联系人后,我希望能够检索这三件事。

我已经成功地从联系人选择器中检索到联系人 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
4

1 回答 1

1

您的光标正在尝试访问-1 索引,因此出现错误。cursor.moveToFirst()在尝试从列中获取详细信息之前尝试使用。

于 2012-09-01T16:55:30.490 回答