Reputation: 1465
I have a lot of PNG images contained in the various Drawable folders (Drawable-xhdpi, Drawable-mdpi, etc.) They are all relatively small (at most like 10KB) but at one point I need to load about 50 of them onto the screen. When I do this, it causes an OutOfMemoryError. Ideally, I would like to be able to load these images by simply calling setContentView once (the content view has a bunch of ImageViews with their src attribute already set to the corresponding images). That's what I'm doing now, but of course this isn't working because of the memory error. Besides reducing the size of the images, is there any way to prevent the OutOfMemoryError?
Upvotes: 0
Views: 1154
Reputation: 26
Avoid loading this number of images all at once,
instead you can load them in a GridView
as described here.
Use Picasso with GridView
for memory efficiency
public View getView(int position, View convertView, ViewGroup container) {
ImageView imageView;
if (convertView == null) { // if it's not recycled, initialize some attributes
imageView = new ImageView(mContext);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setLayoutParams(new GridView.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
} else {
imageView = (ImageView) convertView;
}
// Load image into ImageView "using Picasso"
Picasso.with(mContext).load(imageResIds[position]).into(imageView);
return imageView;
}
Upvotes: 1