user3748230
user3748230

Reputation:

Create bitmap out of memory in android

I have the following code to create a canvas with a size of 8303 × 5540, but running that code produces a OutOfMemoryException.

scaledBitmap = Bitmap.createBitmap(8303, 5540, Bitmap.Config.ARGB_8888);

How can I resolve this problem?

Upvotes: 3

Views: 6164

Answers (3)

Hammad Tariq
Hammad Tariq

Reputation: 13481

http://codingaffairs.blogspot.com/2016/07/processing-bitmap-and-memory-management.html

Now here are tips which you can follow and can avoid out of memory exception in your Android Application.

  1. Always use inSampleSize

Now what is inSampleSize ? with the help of inSampleSize you are actually telling the decoder not to grab every pixel in memory, instead sub sample image. This will cause less number of pixels to be loaded in memory than the original image. you can tell decoder to grab every 4th pixel or every second pixel from original image. if inSampleSize is 4. decoder will return an Image that is 1/16 the number of pixels in original image.

so how much memory you have saved ? calculate :)

  1. Read Bitmap Dimensions before loading into memory.

    How reading bitmap dimensions before loading image into memory can help you avoid out of
    memory error ? Let's Learn

    use inJustBounds = true

here is technique with the help of which you can get image dimension beore loading it in memory

 BitmapFactory.Options options = new BitmapFactory.Options();
 options.inJustDecodeBounds = true;
 Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.id.myimage,    options);
 int imageHeight = options.outHeight;
 int imageWidth = options.outWidth;
 String imageType = options.outMimeType;

Above code snippet will not give us any image/bitmap. it will return null for bitmap Object. but it will definitely return width and height of that image. which is R.id.myimage.

Now you have width and height of Image. you can scale up or scale down image based on these factors:

  • ImageView size which will be used to display Image.
  • Available amount of memory. you can check available amount of memory using ActivityManager and getMemoryClass.
  • Screen size and density of device.

    1. Use appropriate Bitmap Configuration

    Bitmap configurations is color space/color depth of an Image. Default bitmap Configuration in Android is RGB_8888 which is 4 bytes per pixel.

If you use RGB_565 color channel which use 2 Bytes per pixel. half the memory allocation for same resolution :)

  1. Use inBitmap property for recycling purpose.

  2. Do not make static Drawable Object as it cannot be garbage collected.

  3. Request large heap in in manifest file.

  4. Use multiple processes if you are doing lot of image processing(memory intensive task) or use NDK (Native Development using c, c++)

Upvotes: -2

user1795683
user1795683

Reputation: 196

Setting android:largeHeap="true" in AndroidManifest.xml helped me.

Upvotes: 5

baske
baske

Reputation: 1352

Well.. Creating a bitmap of that size, you would have to allocate about 183MB of memory. That will be a problem on most phones. You could try to set android:largeHeap="true" in your manifest, but still that will not give you enough memory on most phones.

If you are willing to accept a "subsampled" version of your image, and the image data is coming from file, you could take a look at http://developer.android.com/training/displaying-bitmaps/load-bitmap.html for loading subsamples of large images into memory. Basically, you can tell the BitmapFactory to load one out of every X pixels, thereby avoiding the requirement to have all 183MB of image data in memory.

Upvotes: 4

Related Questions