Load a contact's picture into a listview instead of the default?

会有一股神秘感。 提交于 2019-12-06 07:15:33
user2696284

I believe you have a mistake. The

long phId=mCursor.getLong(id);

should be part of the while loop.

alyon2002

Try using your Model to hold the picture..

public class ContactStock {

    private Bitmap picture;

    // your current code goes here       

    public void setPicture(Bitmap picture) {
        this.picture = picture;
    }

    public Bitmap getPicture() {
        return picture;
    }
}

make sure you implement the getPicture() method in your ArrayAdapter, you only use your name and number object there, in your xml for your row you have the imageview there already, just add it to the ArrayAdapter ... so how do you get the bitmap?? this worked for me...

public 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); 
}

StackOverflow member "wrongmissle" posted this here. How do I load a contact Photo? thanks wrongmissle. Since you can already get the name and number of a contact just pull the id, plug it into that method with a ContentResolver, that returns a bitmap that can be placed on your list! hope that helped.

The above answer might give you error... Here is a 100% working code

Uri uri = Uri.parse(url);
                             Bitmap bitmap = null;
                            try {
                                bitmap = MediaStore.Images.Media.getBitmap(contexts.getContentResolver(), uri);
                            } catch (FileNotFoundException e) {
                                // TODO Auto-generated catch block
                                bitmap = null;
                                e.printStackTrace();
                            } catch (IOException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                                bitmap = null;
                            }


                            if(bitmap != null)
                                friend.setImageURI(uri);
                            else
                                friend.setImageResource(R.drawable.avatar_photo); 

Where ImageView is friend.

Asaf Manassen

first you need to add a bitmap to the ContactStock class, this will save the contact photo.

change the end of the adapter class to the following code:

        ContactStock currentStock = (ContactStock) stocks.get(position);
        sv.name.setText(currentStock.getName());
        sv.number.setText(currentStock.getNumber());
        sv.image.setImageBitmap(currentStock.getPicture());


        // TODO Auto-generated method stub
        return rowView;
    }

    protected static class ContactStockView {
        protected TextView name;
        protected TextView number;
        protected ImageView image;
    }

to the main activity (AddlistFromContact) add the following function:

public Bitmap getPhoto(long userId ) {
        Uri photoUri = null;
        ContentResolver cr = this.getContentResolver();
        photoUri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, userId);
        Bitmap defaultPhoto = BitmapFactory.decodeResource(getResources(), R.drawable.contacts);
        if (photoUri != null) {
            InputStream input = ContactsContract.Contacts.openContactPhotoInputStream(
                    cr, photoUri);
            if (input != null) {
                return BitmapFactory.decodeStream(input);
            }
        } else {

            return defaultPhoto;
        }

        return defaultPhoto;
    }

now all you need to do is add to your onCreate() function this two lines, after you have defined the cursor of course:



    int id=mCursor.getColumnIndex(Phone.CONTACT_ID);
    long phId=mCursor.getLong(id);

and call the constractur of contactStock in the following way:

contactstock.add(new ContactStock(phName, phNumber,getPhoto(phId)));

hope I've helped

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