我有大约300张jpg图像,我想将这些图像加载到网格视图中。我正在关注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多张图像)加载到网格视图中?
由于
答案 0 :(得分:1)
要解决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);
答案 1 :(得分:1)
For solving OutOfMemory use below link library :-
Download jar and put in libs folder.
http://square.github.io/picasso/
then you should use below code to load image into imageview either from url or drawable folder.
Picasso.with(context).load("your url or drawable image path").into(imageView);
This is best library for solving outofmemory issues:-
答案 2 :(得分:0)
查看持有人将您的布局保存在内存中以节省时间和处理以再次充气视图,因此如果您要加载大量图像,那么它将填满您的内存并让您内存不足。 解决此问题的最佳方法是尝试将图像重新缩放为小分辨率图像,并将其设置为imageview或删除视图持有者。 还有一个建议是在AndoirdManifest.xml中的application标签中添加largeHeap = true和hardwareAccelerated = true
android:hardwareAccelerated="true"
android:largeHeap="true"