Joe
Joe

Reputation: 76

An OutOfMemoryError happen when clip a bitmap use BitmapFactory.createBitmap

I am writing a camera module, which use to take picture.

As the preview's width/height is different from picture's width/height, I need to clip the picture when taken picture, to make it same as what you see in screen.

I use this:

mCamera.takePicture(null, null, mJpegCallback);

to set the picture callback, and then decode and clip the bitmap in callback:

Bitmap originBitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
...
Bitmap bitmap = Bitmap.createBitmap(originBitmap, 0, 0, clipWidth, clipHeight);

However, an OutOfMemoryError happen when create the clip bitmap.I have tried many ways to reuse the data instead of copying a new bitmap, but it doesn't work. So, please anybody can hep me?

Upvotes: 0

Views: 179

Answers (3)

Joe
Joe

Reputation: 76

Thandk you all above!! I solve the problem, android has a class named BitmapRegionDecoder can meet my need. I can only decode the specified region instead of decoding the whole bitmap, and the create a new clipped bitmap, which the latter will easily cause OOM.

Upvotes: 1

anjaneya
anjaneya

Reputation: 708

Add android:LargeHeap=true in manifest file under application tag.

Upvotes: 1

Pontus Backlund
Pontus Backlund

Reputation: 1017

Lot of OutOfMemory exceptions on images, search for it. It's probably because the image is too large for memory. A good place to start when you receive the image is:

    BitmapFactory.options options = new BitmapFactory.options();
    options.inJustDecodeBound = true;
    //this allows you to edit the size of the image before trying to load it into memory

Upvotes: 0

Related Questions