Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android get contact picture, name, and mobile number

I'm trying to retrieve a contact picture, contact name, and mobile phone number from a contact picker. Once I select the contact in the contact picker, I'd like to be able to retrieve those three things.

I've been successfully able to retrieve the contact ID from the contact picker, but I can't seem to get anything else to work using that ID.

I've looked over all kinds of sample code, and they all use a Cursor, and do different things with the cursor, but I don't understand how it all works. Can someone help me out?

Here's some code that I've 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);
                //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");
    }
}

It works up until the cursor is assigned. I get the ID properly, but then I get a FC when I try to retrieve the name, number, or contact picture.

This is what I get from a 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
like image 501
Randy Avatar asked Nov 13 '22 23:11

Randy


1 Answers

Your cursor is trying to access -1 index and hence the error. Try using cursor.moveToFirst() before trying to get the details from the columns.

like image 173
Swayam Avatar answered Dec 12 '22 02:12

Swayam