我有大约 300 张 jpg 图像,我想将这些图像加载到 GridView 中。我正在关注 tutorial
但是当我尝试加载图像时(如果它小于 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 多张图像)加载到我的 GridView 吗?
谢谢
最佳答案
要解决 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);
关于android - 将多个图像加载到gridview android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25801115/