Reputation: 27095
I'm writing a camera app which disables auto-rotation during the camera preview by setting android:screenOrientation="nosensor"
. However, I'd still like to know the rotation of the phone when the picture is taken. getResources().getConfiguration().orientation
is not specific enough, only returning portrait, landscape or square. getWindowManager().getDefaultDisplay().getRotation()
is always 0 since the screen is forced to be in its default orientation - how can I get what that value would be if auto-rotation were on?
Upvotes: 2
Views: 3135
Reputation: 3296
This is a bit in depth but worked well for tracking the orientation changes. I used it to get the photo orientation when the camera intent was opened and a photo was taken. I then knew what orientation the photo was. It obviously doesn't work for the photo library but you can use exif information for that. I discovered, much to my horror, that the Samsung Galaxy S3 and S4 were not returning exif orientation data from the camera.
//keep a history
private int [] _orientationHistory = new int[5];
private int _orientationIndex;
private int _highestIndex = -1;
private int _currentOrientation;
//essentially compute the mode of the data. This could be improved
protected int getOrientation()
{
if (_highestIndex < 0) return 0;
Arrays.sort(_orientationHistory);
return _orientationHistory[_highestIndex / 2];
}
OrientationEventListener _imageViewOrientationListener = new
OrientationEventListener(getContext(), SensorManager.SENSOR_DELAY_NORMAL) {
public void onOrientationChanged(int angle) {
//angle comes in 360 degrees, convert that to a 0-3 rotation
//Get the angle in 90 degree increments, 0,90,180,270
//with 45 degrees tolerance in each direction (straight up is 0)
//this is the same as the data coming from getWindowManager().getDefaultDisplay().getRotation()
angle = angle + 45;
if (angle > 360) angle = angle - 360;
int orientation = angle / 90;
//I use a history in order to smooth out noise
//and don't start sending events about the change until this history is filled
if (_orientationIndex > _highestIndex) {
_highestIndex = _orientationIndex;
}
_orientationHistory[_orientationIndex] = orientation;
_orientationIndex ++;
if (_orientationIndex == _orientationHistory.length) {
_orientationIndex = 0;
}
int lastOrientation = _currentOrientation;
//compute the orientation using above method
_currentOrientation = getOrientation();
if (_highestIndex == _orientationHistory.length - 1 && lastOrientation != _currentOrientation) {
//enough data to say things changed
orientationChanged(lastOrientation, _currentOrientation);
}
}
};
Then to enable this listener just add this line of code:
if (_imageViewOrientationListener != null) {
_imageViewOrientationListener.enable();
}
Lastly, add the method orientationChanged() and receive updates when the orientation changed. If you need to as I did, start recording this info when you've opened the camera activity and if the orientation flipped to something different, say landscape from portrait, you can assume the pic taken during that time period is the rotation change you recorded.
protected void orientationChanged(int lastOrientation, int currentOrientation) {
Log.d(getClass().getSimpleName(), "Orientation changed to " + currentOrientation + " from " + lastOrientation);
}
Upvotes: 6
Reputation: 21
You can listen to configuration changed
<activity
...
android:configChanges="orientation|screenSize"
...
In onConfigurationChanged() you can force the orientation you want (e.g portrait) but getting information about orientation changes.
@Override
public void onConfigurationChanged(Configuration newConfig) {
//get new configuration orientation from newConfig.orientation
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
And don't use android:screenOrientation="portrait" so when user change the orientation onConfigurationChanged will be called.
Upvotes: 1
Reputation: 665
In activity you can use
@Override
public void onConfigurationChanged(Configuration newConfig) { //this method return you latest configuration
// TODO Auto-generated method stub
super.onConfigurationChanged(newConfig);
// newConfig.orientation //Using this you will able to get orientation of device
}
This method calls after you changed device configuration.
Upvotes: 1