OutOfMemoryError: large increase in Android bitmap (90 ยฐ)

I have a big memory problem:

// in sourceImage is a big JPEG previously loaded Matrix mat = new Matrix(); mat.postRotate(90); Bitmap rotatedImage = Bitmap.createBitmap(sourceImage, 0, 0, sourceImage.getWidth(), sourceImage.getHeight(), mat, true); 

I always run this code, my application crashes and says: "VM will not allow us to allocate xxxxxx bytes"

Can you help me?

Edit:

I saw a lot of similar questions here, but I donโ€™t know how to process the sourceImage before rotating it ... (because the second instance is large in size to hold it at the same time)

Thanks.

+4
source share
2 answers

You cannot create a new rotated bitmap without storing temporary bitmaps in memory.

But you can display the bitmap rotated without creating a new bitmap (apply the transform).

ImageView is not rotatable, so you must write your own extended version of ImageView (RotatedImageView?).

The idea is to override the onDraw method with something like this (not tested).

 @Override public void onDraw(Canvas canvas) { canvas.rotate((int)(angle * 180 / Math.PI), getWidth() >> 1, getHeight() >> 1); super.onDraw(canvas); } 
+2
source

For others like me:

there is an option for the camera to rotate, fooobar.com/questions/1382272 / ...

+1
source

Source: https://habr.com/ru/post/1382270/


All Articles