我想从联系人选择器中获取联系人图片、联系人姓名和手机号码,我已经成功地从联系人选择器中获取了联系人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