Create out of memory bitmap in android

I have the following code to create an 8303x5540 canvas, but this code is generated when I run it OutOfMemoryException

.

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

      

How can I solve this problem?

+3


source to share


3 answers


Setting android:largeHeap="true"

in AndroidManifest.xml

helped me.



+5


source


Well .. Having created a bitmap that size, you would need to allocate about 183MB of memory. This will be a problem for most phones. You can try setting android: largeHeap = "true" in your manifest, but that still won't give you enough memory on most phones.



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

+4


source


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

Now, here are some tips you can use and can avoid out of memory exception in your Android app.

  1. Always use inSampleSize

Now what is inSampleSize? with inSampleSize you are actually telling the decoder to miss every pixel in memory, not a sub-sample image. This will result in fewer pixels to be loaded into memory than the original image. you can say that the decoder captures every 4th pixel or every second pixel from the original image. if inSampleSize is 4. the decoder will return an image equal to 1/16 of the number of pixels in the original image.

So how much memory have you saved? calculate :)

  1. Read the sizes of the bitmaps before loading into memory.

    How to read the size of bitmaps before loading the image into memory can help avoid using a memory error? Let's learn

    use inJustBounds = true

here is a technique with which you can get the size of an image by loading it into 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;

      

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

Now you have the width and height of the image. you can scale or reduce the image based on these factors:

  • The size of the ImageView to be used to display the image.
  • Available memory space. you can check the available memory using ActivityManager and getMemoryClass.
  • Device screen size and density.

    1. Use the appropriate Bitmap configuration

Bitmap configurations are the color space / color depth of the image. The default bitmap configuration in Android is RGB_8888, which is 4 bytes per pixel.

If you are using RGB_565 color channel which uses 2 bytes per pixel. half memory allocation for the same resolution :)

  1. Use the inBitmap property for disposal purposes.

  2. Do not create a static Drawable object as it cannot be garbage collected.

  3. Request a large heap in the manifest file.

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

-1


source







All Articles