java.lang.RuntimeException:无法获取摄像头信息

时间:2014-03-13 15:42:33

标签: android

在我的PhotoCaptureActivity.java的onCreate()中,我有:

OrientationEventListener oeL = new  OrientationEventListener(this) {        
            public void onOrientationChanged(int orientation) {
                 if (orientation == ORIENTATION_UNKNOWN) return;
                 try {
                     android.hardware.Camera.CameraInfo info =
                            new android.hardware.Camera.CameraInfo();                
                     android.hardware.Camera.getCameraInfo(currentCameraId, info);
                     orientation = (orientation + 45) / 90 * 90;
                     int rotation = 0;
                     if (info.facing == CameraInfo.CAMERA_FACING_FRONT) {
                         rotation = (info.orientation - orientation + 360) % 360;
                     } else {  // back-facing camera
                         rotation = (info.orientation + orientation) % 360;
                     }
                     if(camera != null) {
                         Camera.Parameters parameters = camera.getParameters();
                         parameters.setRotation(rotation);
                         savingRotation = rotation;
                     }
                 } catch(Exception e) {
                     return;
                 }
            }
         };
         oeL.enable();

此行发生异常:android.hardware.Camera.getCameraInfo(currentCameraId, info);

java.lang.RuntimeException: Fail to get camera info
       at android.hardware.Camera.getCameraInfo(Camera.java)
       at cic.signin.macio.jun.activities.PhotoCaptureActivity$3.onOrientationChanged(PhotoCaptureActivity.java:70)
       at android.view.OrientationEventListener$SensorEventListenerImpl.onSensorChanged(OrientationEventListener.java:143)
       at android.hardware.SensorManager$ListenerDelegate$1.handleMessage(SensorManager.java:584)
       at android.os.Handler.dispatchMessage(Handler.java:99)
       at android.os.Looper.loop(Looper.java:137)
       at android.app.ActivityThread.main(ActivityThread.java:4424)
       at java.lang.reflect.Method.invokeNative(Method.java)
       at java.lang.reflect.Method.invoke(Method.java:511)
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
       at dalvik.system.NativeStart.main(NativeStart.java)

我的问题是:如果在OrientationEventListener的onOrientationChanged()中调用android.hardware.Camera.getCameraInf(currentCameraId,info)过于频繁而导致异常?如何解决这个问题?顺便说一句,4.0和4.1上没有发生这种异常,只发生在某些设备上。

1 个答案:

答案 0 :(得分:3)

狂野猜测 - 它与您的代码无关,请重置设备并重试。 有时我的相机(甚至闪光灯停止工作)和唯一修复它的是重置设备。