0

こんにちは、グリッドビューがあります (2 つのテキスト ボックスと 1 つのイメージビューがあります)。項目はデータベースから取り込まれます。写真のない商品には標準写真を使用します。アプリは 50 個のアイテムで動作しますが、200 個のアイテムではエラーが発生します。

  • logcat* * android.graphics.BitmapFactory.nativeDecodeStream(ネイティブ メソッド) での java.lang.OutOfMemoryError android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:652) での java.lang.OutOfMemoryError

大きなビットマップをロードするためにGoogleが提案するいくつかの方法を試してみましたが、うまくいきませんでした。http://developer.android.com/training/displaying-bitmaps/load-bitmap.html

inJustDecodeBoundsをtrueに設定すると、アプリは動作しますが、すべてのアイテムが空になります..ここに私のコードが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;

}

}

4

1 に答える 1

1

たぶん、ブロブをフルサイズで保存していますか?もしそうなら、あまりにも多くのスペースを浪費しており、これらのメモリの問題を抱えているため、それを行う必要はありません. とにかくユーザーは大きな画像を見ることはないので、縮小された画像をブロブとして保存するために画像の保存をやり直してください。

完全な blob バイト配列をロードしているため、これらの OOE を取得しています。現在の方法では、開発者の記事から記事を利用することはできません。

Stockまた、不必要に保持することになるため、そのオブジェクトに画像をロードしないでください。それよりもむしろ、画像を に保持するビットマップ ローダー メカニズムを使用し、LRUCacheそこで見つからない場合はデータベースから画像を読み込みます。GridViewアダプタgetViewメソッドで、そのイメージをロードするように要求します。で見つかった場合LRUCacheはそこから取得し、AsyncTask を開始して DB から取得しない場合は、LRUCache に追加してユーザーに表示します。

これは、 LRUCache を使用したビットマップのキャッシュに関するリンクです。

于 2013-09-03T07:21:43.973 に答える