我有一个通过拍照填充的ListView。每次我拍照时,照片都会作为ImageView条目添加到列表视图中,连同日期等。拍摄照片时,Uri会被保存到内存中的arraylist中,当显示列表视图时,它会加载从arraylist uris到bitmaps(按比例缩小)的行的图像,然后是imageview的drawables。一切正常,但是,当listview变得更长时,它变得非常慢,有时我会因字节分配错误而内存不足。
这是我在自己的自定义适配器中填充listview视图的地方:
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;
}
//----------------------------------------
答案 0 :(得分:0)
我通过在listview填充之前将位图图像转换为字节数组来解决问题,然后对于listview,我只使用存储在内存中的bytearray来创建drawable并显示它们。没有内存错误。下面是listview的getview的新代码:
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);
byte[] b = Base64.decode(sampleuris.get(position),Base64.DEFAULT);
ByteArrayInputStream is = new ByteArrayInputStream(b);
Drawable d = Drawable.createFromStream(is, "bloodsample");
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;
}