Suggestions to Avoid Bitmap Out of Memory Error

Out of Memory error with Bitmap

OutofMemory occurs when your app exceeds memory allocated in heap. The bitmap is too large to fit in memory ie heap. In such a case you run out of memory. You need to scale down the bitmap and then use the same. For that check the link below

http://developer.android.com/training/displaying-bitmaps/load-bitmap.html.

There is also a blog @ http://android-developers.blogspot.in/2009/01/avoiding-memory-leaks.html (avoiding memory leaks)

 public static Bitmap decodeFile(File f,int WIDTH,int HIGHT){
try {
//Decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeStream(new FileInputStream(f),null,o);

//The new size we want to scale to
final int REQUIRED_WIDTH=WIDTH;
final int REQUIRED_HIGHT=HIGHT;
//Find the correct scale value. It should be the power of 2.
int scale=1;
while(o.outWidth/scale/2>=REQUIRED_WIDTH && o.outHeight/scale/2>=REQUIRED_HIGHT)
scale*=2;

//Decode with inSampleSize
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize=scale;
return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
} catch (FileNotFoundException e) {}
return null;
}

Quoting from the docs

The BitmapFactory class provides several decoding methods (decodeByteArray(), decodeFile(), decodeResource(), etc.) for creating a Bitmap from various sources. Choose the most appropriate decode method based on your image data source. These methods attempt to allocate memory for the constructed bitmap and therefore can easily result in an OutOfMemory exception. Each type of decode method has additional signatures that let you specify decoding options via the BitmapFactory.Options class.

Setting the inJustDecodeBounds property to true while decoding avoids memory allocation, returning null for the bitmap object but setting outWidth, outHeight and outMimeType. This technique allows you to read the dimensions and type of the image data prior to construction (and memory allocation) of the bitmap.

Also check this link for memory management.

https://www.youtube.com/watch?v=_CruQY55HOk

How to avoid outOfMemory Error on a bitmap without reducing the resolution

Are you sure you're using all 10000 images? The complete sprite sheet for most games generally range in the hundreds or lower thousands. In a 640x480 screen you can only put 24 different characters without overlapping, having too many different characters of that size in a single screen all moving around is probably going to be too confusing.

Some things you can do to reduce your spritesheet size is to reduce the framerate of the sprites, so that multiple consecutive game frames will be rendered using the same sprite images. Many older games uses 6-8 frames for run cycles and they look great. Simpler creeps can even cut more and only uses 3-4 images.

Another thing you can do is a smarter character and level design so that you don't actually need all characters at the same time. Put each different character is in their own file and you can load them depending on what you need for a particular level. You can also reuse sprites with different colors to indicate stronger version of another sprite, the recolored sprite do not actually exist in the spritesheet as separate character, instead it is composed at runtime. If your characters have visible equipments, you also don't need to have a sprite for every combination, instead compose the equipment sprites into the character images at runtime.

You can also reduce the color depth of your sprites, mosts handsets supports rendering RGB565 pixel format, and in many cases using the full RGB888 is probably more color than you actually needed.

Also, you should use a lower resolution images for lower DPI handsets (which are generally lower powered as well). In those handsets your 100x100 sprites would look grossly oversized.

Also, you probably don't need 100x100 pixels sized sprites for all objects. Many objects probably would probably be much smaller than that, and you can use a smaller-sized sprites cell size for them.

Out of memory error in android bitmap

You are getting OutOfMemoryError because you haven't recycle
those bitmaps you used

try to recycle those bitmaps after you used them

bitmap = android.provider.MediaStore.Images.Media.getBitmap(cr, imageUri);
photo_new= rotateImage(bitmap, 90);
ByteArrayOutputStream stream = new ByteArrayOutputStream();

photo_new.compress(Bitmap.CompressFormat.JPEG, 100, stream);
byte[] byteArray = stream.toByteArray();
bitmap.recycle();
Intent i = new Intent(getApplicationContext(),new_class.class);
i.putExtra("image", byteArray);

startActivity(i);
byteArray=null;

out of memory error when using bitmap in recyclerview

Hey you don't have to decode drawable to bitmap.

you can do it like this.

imageview.setImageResource(R.drawable.whatever_the_image_is)

Hope this helps you



Related Topics



Leave a reply



Submit