OutOfMemoryException
对Android的屁股来说真的很痛苦
目前我有一个Gallery小部件,我在其中设置了30,256x256px图像,使用BaseAdapter显示它们。
在某些时候,每一秒尝试让我们说,当我想将ArrayList<Bitmap>
列表作为数据加载到图库时,应用程序崩溃。
我知道在内存中保留30个位图是愚蠢的,但是替代方案是什么。我在资源中拥有它们,我需要它们在画廊中展示。
更新(适配器):
public class MyGalleryAdapter extends BaseAdapter {
private Context mContext;
private ArrayList<MyDataObject> data = new ArrayList<MyDataObject>();
public GalleryItemsAdapter(Context context) {
mContext = context;
}
public int getCount() {
return data.size();
}
public DataHolder getItem(int position) {
return data.get(position);
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = LayoutInflater.from(mContext);
convertView = inflater.inflate(R.layout.single_item, null);
ImageView imgView = (ImageView) convertView.findViewById(R.id.single_item_image_view);
imgView.setImageBitmap(data.get(position).bitmap);
return convertView;
}
public void setData(ArrayList<MyDataObject> data) {
this.data = data;
}
}
private class MyDataObject {
public String title;
public Bitmap image;
}
}
更新(错误):
I/dalvikvm( 9073): "main" prio=5 tid=1 RUNNABLE
I/dalvikvm( 9073): | group="main" sCount=0 dsCount=0 obj=0x40a3c460 self=0x12828
I/dalvikvm( 9073): | sysTid=9073 nice=0 sched=0/0 cgrp=default handle=1074185352
I/dalvikvm( 9073): | schedstat=( 0 0 0 ) utm=437 stm=30 core=1
I/dalvikvm( 9073): at android.graphics.Bitmap.nativeCreate(Native Method)
I/dalvikvm( 9073): at android.graphics.Bitmap.createBitmap(Bitmap.java:605)
I/dalvikvm( 9073): at android.graphics.Bitmap.createBitmap(Bitmap.java:551)
I/dalvikvm( 9073): at android.graphics.Bitmap.createScaledBitmap(Bitmap.java:437)
I/dalvikvm( 9073): at android.graphics.BitmapFactory.finishDecode(BitmapFactory.java:524)
I/dalvikvm( 9073): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:499)
I/dalvikvm( 9073): at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:351)
I/dalvikvm( 9073): at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:773)
I/dalvikvm( 9073): at android.content.res.Resources.loadDrawable(Resources.java:1937)
I/dalvikvm( 9073): at android.content.res.TypedArray.getDrawable(TypedArray.java:601)
I/dalvikvm( 9073): at android.view.View.<init>(View.java:2780)
I/dalvikvm( 9073): at android.view.View.<init>(View.java:2717)
I/dalvikvm( 9073): at android.view.ViewGroup.<init>(ViewGroup.java:379)
I/dalvikvm( 9073): at android.widget.RelativeLayout.<init>(RelativeLayout.java:174)
I/dalvikvm( 9073): at java.lang.reflect.Constructor.constructNative(Native Method)
I/dalvikvm( 9073): at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
答案 0 :(得分:0)
为图片创建缩略图。
Thumbnails.getThumbnail(cr, origId, kind, options);
答案 1 :(得分:0)
我猜这里的问题是什么:
在适配器的onCreateView方法中,无论如何都要创建一个新视图。
您要做的是检查参数给出的视图是否已创建:
@override
public abstract View getView (int position, View convertView, ViewGroup parent){
if(view==null)
// inflate the view
else{
// use view how it is
}
.......
return view;
}
答案 2 :(得分:0)
你应该在android开发网站上阅读这篇关于如何处理位图和内存错误的文章
http://developer.android.com/training/displaying-bitmaps/index.html