Why are my contact photos not displaying in listview?

笑着哭i 提交于 2019-12-31 03:06:07

问题


IMPORTANT TO NOTE THAT THE API IM WORKING WITH IS 2.3

I have a listview that is currently populated by the contacts (currently 6) who have sent me text messages in my device's inbox. After all contacts are collected and passed into an ArrayList<String>, the ArrayList is then passed into a constructor for my CustomAdapter class. From there, this is the code that populates my listview with the contacts from my inbox from within my getView() method:

holder.photo = (ImageView) rowView.findViewById(R.id.iv_contactPic);
holder.contact = (TextView) rowView
                .findViewById(R.id.contactEntryText);

String folder = "content://sms/inbox/";
        Uri mSmsQueryUri = Uri.parse(folder);
        contactID = new ArrayList<String>();

        try {
            c = context.getContentResolver().query(mSmsQueryUri,
                    new String[] { "_id", "address", "date", "body" },
                    null, null, null);
            if (c == null) {
                Log.i(TAG, "cursor is null. uri: " + mSmsQueryUri);
            }

                c.moveToFirst();
                while (c.moveToNext()) {

                cid = c.getString(0);
                contactID.add(cid); // stores contact IDs
            }

        } catch (Exception e) {
            //Log.e(TAG, e.getMessage());
        } finally {
            c.close();
        }

if(holder != null){
    holder.contact.setText(data.get(position)); // displays contact by name

    //Contact photo not showing
    holder.photo.setImageBitmap(getByteContactPhoto(contactID.get(position));
}

From the above code, holder.contact displays 6 contacts with no problem. But the problem is with holder.photo which displays nothing at all. Here is the method that is suppose to get the photos:

public Bitmap getByteContactPhoto(String contactId) {
    Uri contactUri = ContentUris.withAppendedId(Contacts.CONTENT_URI, Long.parseLong(contactId));
    Uri photoUri = Uri.withAppendedPath(contactUri, Contacts.Photo.CONTENT_DIRECTORY);
    Cursor cursor = context.getContentResolver().query(photoUri,
                    new String[] {Contacts.Photo.DATA15}, null, null, null);
    if (cursor == null) {
        return null;
    }
    try {
                cursor.moveToFirst();
        if (cursor.moveToNext()) {
            byte[] data = cursor.getBlob(0);
            if (data != null) {
                return BitmapFactory.decodeStream( new ByteArrayInputStream(data));
            }
        }
    } finally {
        cursor.close();
    }
    return null;
    }

But in LogCat, this is the only thing displaying in reference to the photos:

W/Resources(15031): Converting to string: TypedValue{t=0x12/d=0x0 a=3 r=0x7f080015}
W/Resources(15031): Converting to string: TypedValue{t=0x12/d=0x0 a=3 r=0x7f080015}
W/Resources(15031): Converting to string: TypedValue{t=0x12/d=0x0 a=3 r=0x7f080015}
W/Resources(15031): Converting to string: TypedValue{t=0x12/d=0x0 a=3 r=0x7f080015}
W/Resources(15031): Converting to string: TypedValue{t=0x12/d=0x0 a=3 r=0x7f080015}
W/Resources(15031): Converting to string: TypedValue{t=0x12/d=0x0 a=3 r=0x7f080015}

Any ideas as to fix this so that the contact photos are displayed?

来源:https://stackoverflow.com/questions/11029475/why-are-my-contact-photos-not-displaying-in-listview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!