Goal: I want to send the captured image file to server, in code I can send photoFile
to server using retrofit, but the image is rotated on server side, so my goal is to observe rotation, rotate back to real state and post that file to server.
Issue: I capture image from cam and then observe its rotation and based on that I try to rotate it back, but it gives out of memory error while creating second rotated bitmap.
Question: I am looking for solution where I can rotate and send it to server without creating bitmap, if not, avoiding outofmemory error and send correctly rotated file to server.
else if (requestCode == CAPTURE_IMAGE) {
ExifInterface exifInterface = new ExifInterface(photoFile.getAbsolutePath());
Bitmap bitmapOrg = BitmapFactory.decodeFile(photoFile.getAbsolutePath(), new BitmapFactory.Options());
Matrix matrix = new Matrix();
//getRotation method rightly gives 90 as rotation.
matrix.postRotate(getRotation(exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION,
ExifInterface.ORIENTATION_NORMAL)));
//point of outofmemory error while creating this bitmap
Bitmap rotated = Bitmap.createBitmap(bitmapOrg, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
Log.d(TAG, "onActivityResult: ");
}
it gives out of memory error while creating second rotated bitmap
You may not have free system RAM to load in a single full-resolution photo, let alone two of them. Your primary choices are:
Do not load a full-resolution photo. Instead, choose a smaller resolution, using BitmapFactory.Options
and inSampleSize
to have Android scale down the image when loading it in. This does not guarantee that it will work, as your heap may be rather fragmented, but it increases the likelihood that it will work.
Do the rotation on the server, where you have lots of RAM, lots of CPU time, etc.
Load the photos and do the rotation in C/C++ code, using the Android NDK, as native allocations (e.g., malloc()
) do not count against your heap limit.
Use android:largeHeap="true"
in the manifest for your <application>
. On some devices, this will give you a larger heap to work with. There is no guarantee that you will get a larger heap, and even with the larger heap you may still run out of memory.
I am looking for solution where I can rotate and send it to server without creating bitmap
There is nothing in Android for that, sorry.