Android API Version Compatibility

一世执手 提交于 2019-12-09 16:24:06

问题


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



来源:https://stackoverflow.com/questions/4236902/android-api-version-compatibility

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!