I am trying to get a contact picture, contact name and mobile number from the contact selection panel. As soon as I select a contact in the contact picker, I would like to get these three things.
I was able to successfully get the contact id from the contact set, but I cannot get anything to work using this identifier.
I looked through all types of example code and they all use the cursor and perform different actions with the cursor, but I don’t understand how it all works. Can someone help me?
Here is the code I tried:
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);
contactNumber = cursor.getString(cursor.getColumnIndex((ContactsContract.CommonDataKinds.Phone.NUMBER)));
cursor.close();
break;
}
} else {
Log.w("Auto Respond", "Warning: activity result not ok");
}
}
It works until a cursor is assigned. I get the identifier correctly, but then I get the FC when I try to get the name, number or image of the contact.
This is what I get from 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