我根据那里的答案修改了the code in this question,以便在ListView上加载联系人的图片。我正在获取Photo_id,并使用它来获取联系人的位图,使用loadContactPhoto(ContentResolver cr,long id)。问题是没有ImageView获得新图像,尽管照片ID总是不同的。我尝试使用Contact._ID,但仍然只有两个联系人'ImageView有联系人图片,他们都错了。我评论了我在下面添加的新行。
以下是编辑后的代码:
ContactStock:
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;
}
}
addlistfromcontact:
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);
}
}
ContactListAdapter:
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
}
}
答案 0 :(得分:1)
您的代码中存在两个问题。第一个很容易克服,其他人需要更多的工作。
让我们从简单的开始:
方法ContactsContract.Contacts.openContactPhotoInputStream(cr, uri)
采用联系人uri而非照片uri。这是您致ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, id)
的ID必须是联系人ID。
在迭代结果集时,您还会创建许多Bitmap
个对象。不要这样做。虽然这可能最初起作用,但当列表变长时,它可能会因OutOfMemory
错误而崩溃。尝试根据需要创建尽可能少的Bitmap
个对象。即:仅适用于那些可见的行。滚动列表视图时,您必须回收现有的位图。
答案 1 :(得分:0)
由于我在确定如何在没有任何UI冻结的情况下有效加载所有联系人的照片时遇到了很多麻烦,当我第一次开始使用Android时加载图像时出现错误,我强烈建议任何看我的问题的人都要好好看看here
这是一个示例应用程序,它教您如何处理所有处理数据并有效地加载图像,在滚动列表时没有任何“打嗝”。这也是一个让你开始使用Android的好项目!
具体来说,对于我的问题代码的联系人图像部分,API提供了两个简单的方法here,它们返回联系人的高分辨率图像或缩略图的InputStream
。您所要做的就是使用BitmapFactory.decodeByteArray()
对其进行解码。请记住加载您需要在应用中使用的图像大小(上面的链接中描述的内容),这样您就不会得到OutOfMemoryError!
为了进一步改进代码,您还可以使用自定义Cursor适配器替换ArrayAdapter,并且只在现场加载您需要的listView行。