嗨,我有一个 gridview(它有两个文本框和一个 imageview) 这些项目是从 db 填充的。对于没有照片的项目,我使用标准照片。应用程序适用于 50 个项目,但有 200 个项目会出现一些错误。
- logcat* * java.lang.OutOfMemoryError 在 android.graphics.BitmapFactory.nativeDecodeStream(Native Method) 在 android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:652)
我尝试了一些从谷歌获得的加载大位图的方法,但没有奏效。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;
}
}