android照相: getWindowManager().getDefaultDisplay ().getRotation ()总是返回0

在编写一个android照相程序,需要判断屏幕方向,但是下面的代码得到的degrees总是0。

public int getOrientation () {
    int rotation = getWindowManager().getDefaultDisplay ().getRotation ();
    int degrees = 0;
    switch (rotation) {
        case Surface.ROTATION_0:
            degrees = 0;
            break;
        case Surface.ROTATION_90:
            degrees = 90;
            break;
        case Surface.ROTATION_180:
            degrees = 180;
            break;
        case Surface.ROTATION_270:
            degrees = 270;
            break;
    }
    return degrees;

}

    在AndroidManifest.xml文件中,设置:
     android:screenOrientation="portrait"
将android:screenOrientation=换成unspecified,对getOrientation的返回值没有影响。

尝试了用手机传感器获取方向,但是不准。

https://blog.csdn.net/superxukai88/article/details/78657635

应该是手机关闭了自动旋转的功能,打开就行了.