我有一个通过拍照填充的 ListView。每次我拍照时,照片都会作为 ImageView 条目添加到列表视图中,以及日期等。拍摄照片时,Uri 会保存到内存中的数组列表中,当显示列表视图时,它会加载将 arraylist uris 中的行的图像转换为位图(按比例缩小),然后转换为图像视图的可绘制对象。一切正常,但是,当列表视图变长时,它会变得非常慢,有时我会因字节分配错误而内存不足。
这是我在自己的自定义适配器中填充列表视图的视图:
public View getView(int position, View convertView, ViewGroup parent)
{
Uri selectedImage2 = Uri.parse(sampleuris.get(position).toString());
Bitmap ThumbImage = null;
if(convertView == null)
{
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.row_list_sample_entry, parent , false);
}
ImageView sampleimage = (ImageView) convertView.findViewById(R.id.sampleimage);
if(ThumbImage == null)
{
try
{
ThumbImage = ThumbnailUtils.extractThumbnail(decodeUri(selectedImage2), 100, 100);
}
catch (FileNotFoundException ex)
{
Logger.getLogger(Screen_View_Package.class.getName()).log(Level.SEVERE, null, ex);
}
Drawable d = new BitmapDrawable(null, ThumbImage);
sampleimage.setImageDrawable(d);
}
else
{
Drawable d = new BitmapDrawable(null, ThumbImage);
sampleimage.setImageDrawable(d);
}
TextView position2 = (TextView) convertView.findViewById(R.id.packageimage2);
TextView datereceived = (TextView) convertView.findViewById(R.id.date);
position2.setText(""+(position+1));
datereceived.setText(sampledates.get(position));
//textSynced.setText("Synced: "+syncs.get(position));
return convertView;
}
//----------------------------------------