Recently in the camera function, simple packaging system cameras, encountered a problem is that unable to get the current orientation of the screen caused the screen to rotate after the correct direction, so the picture is not in the right direction.
Check it out on the Internet. The current screen orientation is obtained through the WindowManager display or the Getorientation method of the configuration. But the problem is that the two methods require the activity not to be in a fixed direction and require the system to turn on automatic rotation to get the correct value, otherwise a fixed value will be returned.
Search to search is only these two answers, I think I can only use the monitoring direction sensor to achieve, but Android orientation sensor on the screen direction is almost unsupported. Finally in the API document Orientationeventlistener this thing, look at the document and think this thing is too scientific, more than using windowmanager/configuration/orientationsensor science , but the online can not find the introduction of his article, it is a pity!
To implement Orientationeventlistener, you need to create a member variable:
Orientationeventlistener Mscreenorientationeventlistener;
To initialize him:
this.mScreenOrientatio Neventlistener = new Orientationeventlistener (mactivity) {@Override public void Onorientationchange d (int i) {//I's range is 0~359///screen left at the top of the time I = 90; The top of the screen at the bottom of the time i = 180; Screen right at the bottom of the time I = 270; Normal condition default i = 0; if (<= i && i < 135) {mscreenexiforientation = exifinterface.orientation_rotate_180; } else if (135 <= i && i < 225) {mscreenexiforientation = Exifinterface.ori entation_rotate_270; } else if (225 <= i && i < 315) {mscreenexiforientation = Exifinterface.orientation_norma L } else {mscreenexiforientation = exifinterface.orientation_rotate_90; } } };
This way, regardless of whether the screen has a fixed orientation, regardless of whether the system is set to allow automatic rotation of the screen can get to the current direction of the screen!
When you turn on the monitor call listener.enable (), do not need to listen to the time listener.disable () just fine.
Android Force Get screen orientation