Your algorithm helps me a lot saving memory in my app, Thank you; I used your algorithm like this:
public Bitmap getRotatedBitmap(Bitmap bitmap)
{
JniBitmapHolder bitmapHolder = new JniBitmapHolder();
bitmapHolder.storeBitmap(bitmap);
bitmapHolder.rotateBitmapCw90();
bitmap.recycle();
return bitmapHolder.getBitmapAndFree();
}
Now, the next step is save this bitmap without lose quality. I'll try this:
public void saveBitmap1(Bitmap bitmap)
{
bitmap = getRotatedBitmap(bitmap);
String name = "desiredFilename.png";
FileOutputStream fos = openFileOutput(name, Context.MODE_PRIVATE);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
fos.flush();
fos.close();
}
The algorithm works fine, but this is too slow when the bitmap is compressing in PNG or WEBP. JPEG with 100 quality it's faster, anyway, the bitmap will lost quality. I'll try this too
public void saveBitmap2(Bitmap bitmap)
{
bitmap = getRotatedBitmap(bitmap);
int bytes = bitmap.getByteCount();
ByteBuffer buffer = ByteBuffer.allocate(bytes);
bitmap.copyPixelsToBuffer(buffer);
byte[] array = buffer.array();
String name = "desiredFilename.png";
FileOutputStream fos = openFileOutput(name, Context.MODE_PRIVATE);
fos.write(array);
fos.flush();
fos.close();
}
This solution have problems with big bitmaps, i.e. 5312 x 2988. When the buffer is allocating the bytes throws an OutOfMemoryException. And I can't resize the picture; I must rotate the picture without resize or lost quality.
So, Is there a form to get a byte array from your native library? Or Could you help me how I can save the bitmap as a File, avoiding memory exceptions?
I'll be very grateful if you can solve my problem