我大约有300张jpg图片,我想将此图片加载到网格视图中。我正在关注一个教程
但是,当我尝试加载图像时(如果它小于25很好),那么我将收到内存不足的错误。
private ArrayList getData() {
final ArrayList imageItems = new ArrayList();
// retrieve String drawable array
TypedArray imgs = getResources().obtainTypedArray(R.array.image_ids);
for (int i = 0; i < imgs.length(); i++) {
Bitmap bitmap = BitmapFactory.decodeResource(this.getResources(),
imgs.getResourceId(i, -1));
imageItems.add(new ImageItem(bitmap, "Image#" + i));
}
return imageItems;
}
这是加载图像和
customGridAdapter = new GridViewAdapter(this, R.layout.row_grid, getData());
gridView.setAdapter(customGridAdapter);
这是设置图像的代码。你们知道我如何将所有(200多个图像)加载到网格视图吗?
谢谢
要在android.graphics.BitmapFactory.nativeDecodeByteArray处解决java.lang.OutOfMemoryError异常,应使用以下代码:
BitmapFactory.Options options=new BitmapFactory.Options();// Create object of bitmapfactory's option method for further option use
options.inPurgeable = true; // inPurgeable is used to free up memory while required
Bitmap songImage1 = BitmapFactory.decodeByteArray(thumbnail,0, thumbnail.length,options);//Decode image, "thumbnail" is the object of image file
Bitmap songImage = Bitmap.createScaledBitmap(songImage1, 50 , 50 , true);// convert decoded bitmap into well scalled Bitmap format.
imageview.SetImageDrawable(songImage);
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句