deimos1988
deimos1988

Reputation: 6086

Android - get device orientation after taking a photo

I want to get the device orientation, which theoretically should work using this:

getActivity().getResources().getConfiguration().orientation;

However I noticed that sometimes, after I have called the camera using this code:

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
fileUri = getOutputMediaFile(1); // create a file to save the image
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); // set the image file name
// start the image capture Intent
intent.putExtra(GlobalDefines.IMAGE_NR, 1);
startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE_1);

and after I have taken the image, when I try to get the device orientation in onActivityResult (which is called after the picture is taken) the device orientation states "landscape" mode, although I took the picture in portrait mode and the device is held in portrait mode.

The strange thing is that this only occurs in around 3 out of 10 times. It appears that while taking the picture the device orientation is internally changed to "landscape mode" (even though the image is taken in portrait mode), and after the picture is taken, it takes a while until the device realizes that it is in portrait mode.

What can I do about it?

Upvotes: 0

Views: 878

Answers (1)

Alex Cohn
Alex Cohn

Reputation: 57173

If the device does not write reliable EXIF orientation, this may be caused by HW problems with the orientation sensor (accelerometer). There is no sense in checking the orientation in your app if this is the case.

I can explain why sometimes your activity will not report correct (portrait) orientation in onActivityResult() after taking a picture via Intent, and how this orientation can be sensed in a more robust manner. But unfortunately, this will be in vain.

You cannot rely on the orientation of the device at the moment your app receives control back from the camera app, because after the end user actually captures a picture, she will preview it, probably make another attempt, and finally accept (or reject) the photo. During all this lengthy process after capture, the device may be rotated many times, consciously or unconsciously, by the end user. So, if you cannot rely on EXIF orientation, but can rely on the device sensors, you have no choice but use a Camera object, taking the picture within your app.

Now, for the record, here is the outline of explanation I mentioned in the second paragraph.

When your app receives onActivityResult() callback, it may happen after your Activity had been destroyed by the system (to free more memory for the Camera app). You have no control on whether this will happen, but you can know whether this was the case when control is returned back to your app.

In this case, your Activity will not know its orientation yet. It determines this orientation later, just before the onResume() callback. Therefore, you cannot blindly rely on the Activity orientation when inside onActivityResult(). You should read the accelerometer sensor directly, and calculate whether the orientation is landscape or portrait (see OrientationEventListener as examaple).

Unfortunately, this is only a theoretical option, as I explained above. And luckily, when you use a custom Camera, you can simply use the above OrientationEventListener class in your activity to determine the orientation reliably (to the extent the HW sensors are reliable).

Upvotes: 3

Related Questions