Android API Version Compatibility

2020-07-08 07:18发布

问题:

I'd like my app to run on both Android versions 2.1 and 2.2. In one area of my app, there is a portrait-style camera - the process for producing a portrait camera preview is different (as far as I know) on the two OS versions. Here is how:

2.1:

Camera.Parameters parameters = camera.getParameters();
parameters.set("orientation", "portrait");
camera.setParameters(parameters);

2.2:

camera.setDisplayOrientation(90);

the setDisplayOrientation(int) method became available in API Level 8 (2.2) and, so, cannot be used on 2.1; however, using the 2.1 (Camera.Parameters) method does not rotate the preview and image correctly on 2.2.

It seems odd that this incompatibility exists - is there a more correct way to do this that will allow me to target both platforms?

回答1:

Try:

Camera.Parameters parameters = camera.getParameters();
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.FROYO) {
    parameters.setRotation(90);
    camera.setParameters(parameters);
} else {
    camera.setDisplayOrientation(90);
}


回答2:

There's no general way to change the camera to orientation to portrait mode prior to v2.2. The set("orientation", "portrait") works on some devices and not on others.

It seemed odd to me as well.



回答3:

Try to call Activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT) in onConfigurationChanged callback OR discover a source code of Camera.setDisplayOrientation method from Android 2.2 (or 2.3) and try to implement something similar in your application.

See also related question at stackoverflow.com