English 中文(简体)
将联系人图片装入 ListView (Part 2)
原标题:Load a contact s picture into ListView (part 2)

我修改了这个问题的代码 , 以便根据那里的答复, 将联系人的照片装入 ListView 上。 我拿到了相片_ id, 并用它来获取联系人的Bitmap, 使用负载ContactPhoto (ContentResolver cr, long id) 。 问题是没有图像View 获得新的图像, 尽管照片id总是不同的。 我尝试了使用联系人。 _ ID, 但只有两个联系人图像View 获得了联系人的照片, 它们都是错的。 我评论了我在下面添加的新行 。

这是编辑后的代码 :

联系人斯托克 :

public class ContactStock {

private String name;
private String number;
private Bitmap picture;


public ContactStock(String name, String number) {
    this.name = name;
    this.number = number;
}

public ContactStock(String name, String number, Bitmap photo) {
    this.name = name;
    this.number = number;
    this.picture = photo;
}

public void setName(String name) {
    this.name = name;
}

public void setNumber(String number) {
    this.number = number;
}

public String getName() {
    return this.name;
}

public String getNumber() {
    return this.number;
}

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

public Bitmap getPicture() { // NEW METHOD
    return picture;
}
}

添加来自联系人的列表 :

    public class addlistfromcontact extends Activity {
private ListView lst;
private List<ContactStock> contactstock;
private Cursor mCursor;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.tab_contact_list);
    lst = (ListView) findViewById(R.id.tab_contact_list);
    contactstock = new ArrayList<ContactStock>();

    mCursor = managedQuery(ContactsContract.Data.CONTENT_URI, null,
            Data.MIMETYPE + "= " + Phone.CONTENT_ITEM_TYPE + " ", null,
            ContactsContract.Data.DISPLAY_NAME + " ASC");

    int number = mCursor.getColumnIndex(Phone.NUMBER);
    int name = mCursor.getColumnIndex(ContactsContract.Data.DISPLAY_NAME);
    int id = mCursor.getColumnIndex(Contacts.PHOTO_ID); // NEW LINE

    while (mCursor.moveToNext()) {

        String phName = mCursor.getString(name);
        String phNumber = mCursor.getString(number);
        long phId = mCursor.getLong(id); // NEW LINE

        Bitmap phPhoto = loadContactPhoto(getContentResolver(), phId); // NEW LINE
        Log.d("phId=", phId + "");

        contactstock.add(new ContactStock(phName, phNumber, phPhoto)); // NEW LINE EDIT
    }
    lst.setAdapter(new ContactListAdapter(addlistfromcontact.this,
            contactstock));
}

public static Bitmap loadContactPhoto(ContentResolver cr, long id) { // NEW METHOD
    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);
}

    }

联系列表编辑 :

public class ContactListAdapter extends ArrayAdapter {
    private final Activity activity;
    private final List stocks;

    public ContactListAdapter(Activity activity, List objects) {
        super(activity, R.layout.listview_detail_tab_contact_list, objects);
        this.activity = activity;
        this.stocks = objects;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        View rowView = convertView;
        ContactStockView sv = null;
        if (rowView == null) {
            // Get a new instance of the row layout view
            LayoutInflater inflater = activity.getLayoutInflater();
            rowView = inflater.inflate(
                    R.layout.listview_detail_tab_contact_list, null);

            // Hold the view objects in an object,
            // so they don t need to be re-fetched
            sv = new ContactStockView();
            sv.name = (TextView) rowView.findViewById(R.id.contact_name);
            sv.number = (TextView) rowView.findViewById(R.id.contact_number);
            sv.photo = (ImageView) rowView.findViewById(R.id.contact_photo);

            // Cache the view objects in the tag,
            // so they can be re-accessed later
            rowView.setTag(sv);
        } else {
            sv = (ContactStockView) rowView.getTag();
        }
        // Transfer the stock data from the data object
        // to the view objects
        ContactStock currentStock = (ContactStock) stocks.get(position);
        sv.name.setText(currentStock.getName());
        sv.number.setText(currentStock.getNumber());
        sv.photo.setImageBitmap(currentStock.getPicture()); // NEW LINE

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

    protected static class ContactStockView {
        protected TextView name;
        protected TextView number;
        protected ImageView photo; // NEW LINE
    }
}
最佳回答

在你的代码中有两个问题。第一个问题很容易克服,其他问题需要做更多的工作。

Lets start with the easy one: The method ContactsContract.Contacts.openContactPhotoInputStream(cr, uri) takes a contact uri and not a photo uri. That is the id in your call to ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, id) has to be a contact id.

在对结果集进行迭接时,您还创建了许多 Bitmap 对象。 不要这样做。 虽然这在一开始可能有效, 但当列表长时, 它可能会随 < code> Outofmemory 错误而崩溃。 尝试根据需要创建几个 Bitmap 对象。 也就是说: 只有那些可以看见的行。 当滚动列表时, 您必须复制已有的 Bitmaps 。

问题回答

由于我有很多困难, 无法在没有任何UI冻结的情况下, 有效装入所有联系人照片, 以及我刚开始研究Android时在装入图像时的错误, 我强烈建议任何关注我的问题的人都仔细看看