Reputation: 2088
I have a ArrayList with two columns and I display a image with text below. I am using high quality images and need to display those images in the grid view with a decent quality for which I am using BitmapFactory.Options. I am using the same code from google but that still throws me an OutOfMemory error.
CODE :
BitmapFactory.Options obj = new BitmapFactory.Options();
obj.inPurgeable = true;
obj.inJustDecodeBounds = true;
BitmapFactory.decodeResource(getResources(), R.drawable.car, obj);
BitmapFactory.decodeResource(getResources(), R.drawable.nature, obj);
obj.inSampleSize = 4;
obj.inJustDecodeBounds = false;
Bitmap homeIcon = BitmapFactory.decodeResource(getResources(), R.drawable.car,obj);
Bitmap userIcon = BitmapFactory.decodeResource(getResources(), R.drawable.nature,obj);
gridArray.add(new Item(homeIcon,"Home"));
gridArray.add(new Item(userIcon,"User"));
gridArray.add(new Item(homeIcon,"House"));
gridArray.add(new Item(userIcon,"Friend"));
gridArray.add(new Item(homeIcon,"Home"));
gridArray.add(new Item(userIcon,"Personal"));
gridArray.add(new Item(homeIcon,"Home"));
gridArray.add(new Item(userIcon,"User"));
gridArray.add(new Item(homeIcon,"Building"));
gridArray.add(new Item(userIcon,"User"));
gridArray.add(new Item(homeIcon,"Home"));
gridArray.add(new Item(userIcon,"xyz"));
UPDATE :
Item.java :
public class Item {
Bitmap image;
String title;
public Item(Bitmap image, String title) {
super();
this.image = image;
this.title = title;
}
public Bitmap getImage() {
return image;
}
public void setImage(Bitmap image) {
this.image = image;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}
This is the code and I have another class name Item which has a constructor with arguments as Bitmap and String. While executing this, it throws me an OutOfMemoryError. I am not sure whether I should add any other extras in these code. Any help will be appreciated guys.
Upvotes: 0
Views: 581
Reputation: 71
you can modify below code according to your need.I hope it will help you.
Bitmap imageProcess(String path) {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
imageBitmap = BitmapFactory.decodeFile(path, options);
options.inSampleSize = calculateInSampleSize(options,
displayMetrics.widthPixels,
(int) (displayMetrics.heightPixels * .75));
options.inJustDecodeBounds = false;
imageBitmap = BitmapFactory.decodeFile(path, options);
return imageBitmap;
}
public int calculateInSampleSize(BitmapFactory.Options options,
int reqWidth, int reqHeight) {
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
final int heightRatio = Math.round((float) height
/ (float) reqHeight);
final int widthRatio = Math.round((float) width / (float) reqWidth);
inSampleSize = heightRatio > widthRatio ? heightRatio : widthRatio;
}
return inSampleSize;
}
Upvotes: 0
Reputation: 4340
try removing first two decode statements made in fly which are causing the problem as your bitmaps are loaded there without sampling
Upvotes: 0
Reputation: 443
This Error occurs usually when loading large bitmaps. Are the drawables for you ImageButtons high resolution? If so, this is likely the error. You try downsampling them to their appropriate resolution, but for a quick-fix, adding android:largeHeap="true"
under the <application>
tag of your AndroidManifest.xml file can sometimes allow your application to load large images without an out of memory error.
The reason you use the same code from Google yet still receive the out of memory error is not just the high resolution of the bitmaps, but also the large amount you are loading at once.
Adding a small wait between them can spread the load and make a cute little animation depending on you layout, just an idea (But of course don't do it on the UI Thread).
Good Luck!
Upvotes: 1
Reputation: 1468
You can try to use android:largeHeap="true"
inside the application tag on AndroidManifest to avoid some out of memory erros on your app. It will let your app to use more ram.
Upvotes: 0