当gridview有很多项目时,“内存不足”问题

时间:2013-09-03 07:15:35

标签: java android gridview bitmap out-of-memory

嗨,我有一个gridview(它有两个文本框和一个imageview) 这些项目是从db填充的。我为没有照片的物品使用标准照片。应用程序可以使用50个项目,但有200个项目会产生一些错误。

  • logcat * * java.lang.OutOfMemoryError at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:652)

尝试从谷歌建议加载大位图的一些方法,但它没有工作。 http://developer.android.com/training/displaying-bitmaps/load-bitmap.html

当我设置inJustDecodeBounds为true时,app可以正常工作,但所有项目都是空的.. 这是我的代码从db获取信息:

private void refreshList(String sql)
{
    gridArray = new ArrayList<Stock>();
    final Cursor cursor = _SQLite.RawQueryTry(sql, null);
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inSampleSize = 2;

    if (cursor != null)
    {
        if (cursor.moveToFirst())
        {
            for (int i = 0; i < cursor.getCount(); i++)
            {
                byte[] blob = cursor.getBlob(cursor.getColumnIndex("FOTO"));
                Bitmap stockImage = null;
                ByteArrayInputStream inputStream = null;

                if (blob == null)
                {
                    options.inJustDecodeBounds=false;
                    stockImage = BitmapFactory.decodeResource(getApplicationContext().getResources(), R.drawable.foto_yok, options);
                }
                else
                {
                    inputStream = new ByteArrayInputStream(blob);
                    stockImage = BitmapFactory.decodeStream(inputStream, null, options);

                }
                String stockName = cursor.getString(cursor.getColumnIndex("STOK_ADI"));
                String stockNo = cursor.getString(cursor.getColumnIndex("STOK_NO"));
                String stockCode = cursor.getString(cursor.getColumnIndex("STOK_KODU"));
                String stockEntity = cursor.getString(cursor.getColumnIndex("BIRIM"));
                String stockKdvOranı = cursor.getString(cursor.getColumnIndex("KDV_ORANI"));
                String stockRatio = TableUtils.getFieldValue("KATSAYI", "BIRIM", stockEntity, "STOKBIRI");
                String stockAmount = cursor.getString(cursor.getColumnIndex("MIKTAR"));
                gridArray.add(new Stock(stockImage, stockName, stockNo, stockCode, stockKdvOranı, stockEntity, stockAmount, stockRatio, processNo));

                cursor.moveToNext();
            }
        }
    }

    gridAdapter = new AdapterStockGridView(this, R.layout.stockgriditems, gridArray);
    gridView.setAdapter(gridAdapter);

}

和我的适配器类:

public class AdapterStockGridView extends ArrayAdapter<Stock>
{
    Context context;
    int id;
    ArrayList<Stock> stock = new ArrayList<Stock>();

    public AdapterStockGridView(Context context, int id, ArrayList<Stock> stock)
    {
        super(context, id, stock);
        this.id = id;
        this.context = context;
        this.stock = stock;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent)
    {
        View row = convertView;
        RecordHolder holder = null;

        if (row == null)
        {
            LayoutInflater inflater = ((Activity) context).getLayoutInflater();
            row = inflater.inflate(id, parent, false);

            holder = new RecordHolder();
            holder.stockCode = (TextView) row.findViewById(R.id.stockCode);
            holder.stockName = (TextView) row.findViewById(R.id.stockName);
            holder.stockImage = (ImageView) row.findViewById(R.id.stockImage);
            row.setTag(holder);
        }
        else
        {
            holder = (RecordHolder) row.getTag();
        }

        Stock item = stock.get(position);
        holder.stockCode.setText(item.getStockCode());
        holder.stockName.setText(item.getStockName());
        holder.stockImage.setImageBitmap(item.getStockImage());

        return row;

    }

}

static class RecordHolder
{
    TextView stockName;
    TextView stockCode;
    ImageView stockImage;

}

}

1 个答案:

答案 0 :(得分:1)

也许你将blob存储为全尺寸?如果是这样的话,就没有必要这样做了,因为你浪费了太多的空间并且你有这些内存问题。用户无论如何都不会看到大图像,因此重新存储图像以便将缩小的图像存储为blob。

你正在获得这些OOE,因为你正在加载完整的blob字节数组 - 就像现在这样,你无法使用开发人员文章中的文章。

此外,不要在Stock对象中加载图像,因为您将不必要地抓住它们。而不是使用位图加载器机制,将图像保存在LRUCache中,如果没有找到,则从数据库加载它们。在GridView适配器getView方法请求中加载该图像。如果在LRUCache中找到,请从那里获取,如果没有启动AsyncTask从DB获取它,将其添加到LRUCache并将其显示给用户。

这里有a link缓存位图和LRUCache。