I'm trying to rotate a given bitmap by X degrees in order to send it to my server rotated.
I'm using android API8
using this code:
private static void rotateBitmap(Bitmap source, float angle) {
Matrix matrix = new Matrix();
matrix.postRotate(angle);
return Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), matrix,
true);
}
It works on Nexus 5, but on Galaxy Note2 I get the following error:
05-18 09:06:42.696: E/AndroidRuntime(6044): java.lang.OutOfMemoryError
05-18 09:06:42.696: E/AndroidRuntime(6044): at android.graphics.Bitmap.nativeCreate(Native Method)
05-18 09:06:42.696: E/AndroidRuntime(6044): at android.graphics.Bitmap.createBitmap(Bitmap.java:726)
05-18 09:06:42.696: E/AndroidRuntime(6044): at android.graphics.Bitmap.createBitmap(Bitmap.java:703)
05-18 09:06:42.696: E/AndroidRuntime(6044): at android.graphics.Bitmap.createBitmap(Bitmap.java:636)
Than I have read some posts in SOF
and changed to the following code:
private static void rotateBitmap(Bitmap source, float angle) {
Canvas canvas = new Canvas(source);
Matrix matrix2 = new Matrix();
matrix2.setRotate(angle, source.getWidth() / 2, source.getHeight() / 2);
canvas.drawBitmap(source, matrix2, new Paint());
}
but then I have gotten the following error:
Immutable bitmap passed to Canvas constructor
Update
Then I have tried this code:
private static void rotateBitmap(Bitmap source, float angle) {
// Matrix matrix = new Matrix();
// matrix.postRotate(angle);
// return Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), matrix,
// true);
// Bitmap workingBitmap = Bitmap.createBitmap(source);
Bitmap mutableBitmap = source.copy(Bitmap.Config.ARGB_8888, true);
source.recycle();
// Canvas canvas = new Canvas(mutableBitmap);
Canvas canvas = new Canvas();
// Canvas canvas = new Canvas(source);
Matrix matrix2 = new Matrix();
matrix2.setRotate(angle, mutableBitmap.getWidth() / 2, mutableBitmap.getHeight() / 2);
canvas.drawBitmap(mutableBitmap, matrix2, new Paint());
}
and got this error:
05-18 09:42:17.906: E/AndroidRuntime(18788): java.lang.OutOfMemoryError
05-18 09:42:17.906: E/AndroidRuntime(18788): at android.graphics.Bitmap.nativeCopy(Native Method)
05-18 09:42:17.906: E/AndroidRuntime(18788): at android.graphics.Bitmap.copy(Bitmap.java:479)
How can I fix this?