Android Java Gallery 内存不足错误
所以我将图像从 SD 卡加载到 Android Gallery 中。我点击菜单,然后选择它,它通过下面的代码插入到图库中。问题是在大约 5 或 6 个图像之后,我收到内存不足错误 01-04 18:10:35.246: ERROR/AndroidRuntime(10220): java.lang.OutOfMemoryError: 位图大小超出 VM 预算 关于我能做什么的任何想法解决这个问题吗?
public class ImageAdapter extends BaseAdapter{
int mGalleryItemBackground;
public ImageAdapter(Context c) {
mContext = c;
TypedArray typArray = obtainStyledAttributes(R.styleable.GalleryTheme);
mGalleryItemBackground = typArray.getResourceId(R.styleable.GalleryTheme_android_galleryItemBackground, 0);
typArray.recycle();
}
public int getCount(){
return mUrls.length;
}
public Object getItem(int position){
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent){
ImageView i = new ImageView(mContext);
i.setImageURI(mUrls[position]);
i.setScaleType(ImageView.ScaleType.FIT_XY);
i.setLayoutParams(new Gallery.LayoutParams(120, 120));
return i;
}
private Context mContext;
}
So I'm loading images into Android Gallery from SD card. I hit menu and then select and it's inserted into Gallery via code below. Problem is after about 5 or 6 images I get a out of memory error 01-04 18:10:35.246: ERROR/AndroidRuntime(10220): java.lang.OutOfMemoryError: bitmap size exceeds VM budget Any ideas on what I can do to resolve this?
public class ImageAdapter extends BaseAdapter{
int mGalleryItemBackground;
public ImageAdapter(Context c) {
mContext = c;
TypedArray typArray = obtainStyledAttributes(R.styleable.GalleryTheme);
mGalleryItemBackground = typArray.getResourceId(R.styleable.GalleryTheme_android_galleryItemBackground, 0);
typArray.recycle();
}
public int getCount(){
return mUrls.length;
}
public Object getItem(int position){
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent){
ImageView i = new ImageView(mContext);
i.setImageURI(mUrls[position]);
i.setScaleType(ImageView.ScaleType.FIT_XY);
i.setLayoutParams(new Gallery.LayoutParams(120, 120));
return i;
}
private Context mContext;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您要插入图库中的图像的宽度和高度(以像素为单位)是多少?如果它们非常大,可能是 500x500 或更大,那么我建议在将它们添加到图库之前将它们调整为更小且更接近您预期大小的 120x120。
您可以使用以下方法缩小它们 Bitmap.createScaledBitmap。
编辑:请注意,这与让 ImageView 进行缩放不同。 ImageView 将在显示时缩放位图,但将原始位图保留在堆内存中。如果您使用
createScaledBitmap
并丢弃原始位图,那么您将节省大量堆。What is the width and height in pixels of the images you are inserting into the Gallery? If they are very large, perhaps 500x500 or larger then I'd suggest resizing them to something smaller and closer to your intended size of 120x120 before adding them to the Gallery.
You can scale them down using the method Bitmap.createScaledBitmap.
EDIT: Note that this is different from having the ImageView do the scaling. The ImageView will scale the bitmap as it is displayed but keeps the original bitmap in heap memory. If you use
createScaledBitmap
and throw the original bitmap away then you'll be saving a lot of heap.尽量避免使用
将 Context/Activity 存储为类的私有成员是非常糟糕的做法。建议仅通过参数使用上下文/活动。存储 Context 的典型结果是内存泄漏,我相信这演示了您的代码。您可以在此处阅读更多内容。
Try to avoid usage of
It's very bad practice to store Context/Activity as private members of classes. Context/Activity recommended to use only through parameters. Typical result of storing Context is a memory leaks, which I believe demonstrates your code. More you can read here.