Question

I'm trying to display a listview with starred contacts only, with icons and name to a custom listview. So far I've managed to display them correct without the Photo. When I'm trying to include photos I get several errors(because I tried a lot of different approached which found here). My last attempt was by implement Android Developers "*Displaying the Quick Contact Badg*e" lesson's code, and here is the related code:

Uri queryUri = ContactsContract.Contacts.CONTENT_URI;

    String[] projection = new String[] {
            ContactsContract.Contacts._ID,
            ContactsContract.Contacts.DISPLAY_NAME,
            ContactsContract.Contacts.STARRED,
            ContactsContract.Contacts.LOOKUP_KEY,
            ContactsContract.Contacts.PHOTO_THUMBNAIL_URI};
    String selection =ContactsContract.Contacts.STARRED + "='1'";

    Cursor cursor = managedQuery(queryUri, projection, selection,null,null);

    int mIdColumn;
    int mLookupKeyColumn;
    Uri mContactUri;

    mIdColumn = cursor.getColumnIndex(ContactsContract.Contacts._ID);
    // Gets the LOOKUP_KEY index
    mLookupKeyColumn = cursor.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY);

    mContactUri =
            ContactsContract.Contacts.getLookupUri(
                    cursor.getLong(mIdColumn),
                    cursor.getString(mLookupKeyColumn)
            );


    favIcon.assignContactUri(mContactUri);

    // The column in which to find the thumbnail ID
    int mThumbnailColumn;
/*
 * The thumbnail URI, expressed as a String.
 * Contacts Provider stores URIs as String values.
 */
    String mThumbnailUri;

/*
 * Gets the photo thumbnail column index if
 * platform version >= Honeycomb
 */
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
        mThumbnailColumn =
                cursor.getColumnIndex(ContactsContract.Contacts.PHOTO_THUMBNAIL_URI);
        // Otherwise, sets the thumbnail column to the _ID column
    } else {
        mThumbnailColumn = mIdColumn;
    }
/*
 * Assuming the current Cursor position is the contact you want,
 * gets the thumbnail ID
 */
    mThumbnailUri = cursor.getString(mThumbnailColumn);

    Bitmap mThumbnail =
            loadContactPhotoThumbnail(mThumbnailUri);
    favIcon.setImageBitmap(mThumbnail);

    String[] from = {ContactsContract.Contacts.DISPLAY_NAME};
    int to[] = new int[]{
            R.id.ivDefContact,
            R.id.tvContactName
    };

    ListAdapter adapter = new SimpleCursorAdapter(
            this,
            R.layout.favs_list_item,
            cursor,
            from,
            to,
            CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);


    final ListView listStarred = (ListView) findViewById(R.id.lvFavs);

    listStarred.setAdapter(adapter);

}

The error I get with the above code is:

Android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 9 (9 are the number of my Starred Contacts) and points me to line 85 which is:

mContactUri =
            ContactsContract.Contacts.getLookupUri(
                    cursor.getLong(mIdColumn),
                    cursor.getString(mLookupKeyColumn)
            );

If I comment the R.id.ivDefContact, line the Activity with the listview runs fine and display the contact names correct. So the problem is with the Photo implementation. I read some related threads, but I cannot get how it works.

EDIT: Logcat error:

java.lang.RuntimeException: Unable to start activity ComponentInfo android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 6 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2307) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2357) at android.app.ActivityThread.access$600(ActivityThread.java:153) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1247) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:137) at android.app.ActivityThread.main(ActivityThread.java:5226) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:511) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562) at dalvik.system.NativeStart.main(Native Method) Caused by: android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 6 at android.database.AbstractCursor.checkPosition(AbstractCursor.java:424) at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136) at android.database.AbstractWindowedCursor.getLong(AbstractWindowedCursor.java:74) at android.database.CursorWrapper.getLong(CursorWrapper.java:106) at com.example.DialerActivity.onCreate(DialerActivity.java:85) at android.app.Activity.performCreate(Activity.java:5104) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2261) ... 11 more

Was it helpful?

Solution

Problem is here

String[] from = {ContactsContract.Contacts.DISPLAY_NAME};
    int to[] = new int[]{
            R.id.ivDefContact,
            R.id.tvContactName
    }; 

number of items in both (from and to) must be same.

So you can rewrite from as

String[] from = {ContactsContract.Contacts.PHOTO_THUMBNAIL_URI, ContactsContract.Contacts.DISPLAY_NAME};

OTHER TIPS

I've managed to display photos with the above changes:

Uri queryUri = ContactsContract.Contacts.CONTENT_URI;

    String[] projection = new String[] {
            ContactsContract.Contacts._ID,
            ContactsContract.Contacts.LOOKUP_KEY,
            ContactsContract.Contacts.PHOTO_THUMBNAIL_URI,
            ContactsContract.Contacts.DISPLAY_NAME,
            ContactsContract.Contacts.STARRED};

    String selection =ContactsContract.Contacts.STARRED + "='1'";

    Cursor cursor = managedQuery(queryUri, projection, selection,null,null);

    long id= cursor.getColumnIndex(ContactsContract.Contacts._ID);

    Bitmap bitmap = loadContactPhoto(getContentResolver(), id);
    if(bitmap!=null){
    favIcon.setImageBitmap(bitmap);
    }
    else{

    }

    String[] from = {ContactsContract.Contacts.PHOTO_THUMBNAIL_URI, ContactsContract.Contacts.DISPLAY_NAME};
    int to[] = new int[]{
            R.id.ivDefContact,
            R.id.tvContactName
    };

    ListAdapter adapter = new SimpleCursorAdapter(
            this,
            R.layout.favs_list_item,
            cursor,
            from,
            to,
            CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);


    final ListView listStarred = (ListView) findViewById(R.id.lvFavs);

    listStarred.setAdapter(adapter);

    public static Bitmap loadContactPhoto(ContentResolver cr, long  id) {
    Uri uri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, id);
    InputStream input = ContactsContract.Contacts.openContactPhotoInputStream(cr, uri);
    if (input == null) {

        return null;
    }
    return BitmapFactory.decodeStream(input);
}

Now photos displayed correct for the contacts which has a photo.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top