0

我有一个通过拍照填充的 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;
      }
    //----------------------------------------
4

1 回答 1

0

我通过在填充列表视图之前将位图图像转换为字节数组来解决问题,然后对于列表视图,我只使用存储在内存中的字节数组来创建可绘制并显示它们。没有内存错误。下面是 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;
      }
于 2013-09-16T08:42:18.010 回答