简体   繁体   English

相机从纵向倾斜到横向时发生RuntimeException

[英]RuntimeException when camera is tilted from portrait to landscape

I have a Fragment which uses a CameraPreview like so: 我有一个使用CameraPreview的片段, CameraPreview所示:

    mCamera = getCameraInstance();
    mPreview = new CameraPreview(getActivity(), mCamera);
    FrameLayout preview = (FrameLayout) rootView.findViewById(R.id.camera_preview);
    preview.addView(mPreview);

This works fine - opens up a camera view as intended. 效果很好-可以按预期打开相机视图。 However, when I tilt the camera from portrait to landscape I get a RuntimeException 但是,当我将照相机从纵向倾斜到横向时,会出现RuntimeException

java.lang.RuntimeException: Fail to connect to camera service
        at android.hardware.Camera.native_setup(Native Method)
        at android.hardware.Camera.<init>(Camera.java:413)
        at android.hardware.Camera.open(Camera.java:384)
        at com.example.sv_laptop03.myApp.CameraPreview.surfaceCreated(CameraPreview.java:36)
        at android.view.SurfaceView.updateWindow(SurfaceView.java:610)
        at android.view.SurfaceView.access$000(SurfaceView.java:93)
        at android.view.SurfaceView$3.onPreDraw(SurfaceView.java:182)
        at android.view.ViewTreeObserver.dispatchOnPreDraw(ViewTreeObserver.java:864)
        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2142)
        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1249)
        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6364)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:791)
        at android.view.Choreographer.doCallbacks(Choreographer.java:591)
        at android.view.Choreographer.doFrame(Choreographer.java:561)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:777)
        at android.os.Handler.handleCallback(Handler.java:730)
        at android.os.Handler.dispatchMessage(Handler.java:92)
        at android.os.Looper.loop(Looper.java:176)
        at android.app.ActivityThread.main(ActivityThread.java:5419)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:525)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)
        at dalvik.system.NativeStart.main(Native Method)

and it specifically complains about the first line in the else block of this method: 它专门抱怨此方法的else块中的第一行:

    public void surfaceCreated(SurfaceHolder holder) {
    try {
        if(mCamera != null) {
            mCamera.setDisplayOrientation(90); // WITHOUT THIS MY CAMERA AXIS' ARE FLIPPED
            mCamera.setPreviewDisplay(holder);
            mCamera.startPreview();
        } else {
            mCamera = Camera.open(); // RUNTIME EXCEPTION HERE
            mCamera.startPreview();
        }
    } catch (IOException e) {
        Log.d("TAG", "Error setting camera preview: " + e.getMessage());
    }
}

I included permissions in AndroidManifest 我在AndroidManifest中包含了权限

<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus"/>

Does anybody know what could be causing the crash? 有人知道是什么原因导致车祸吗? It seems to work fine on a Galaxy S6 but crashes on my S3. 它在Galaxy S6上似乎工作正常,但在我的S3上崩溃。 Any help is highly appreciated. 非常感谢您的帮助。

Edit: 编辑:

I added this in my Activity, now the app crashes before the camera loads. 我在“活动”中添加了此内容,现在该应用在加载相机之前崩溃了。

 @Override
public void onPause() {
    super.onPause();
    mCamera.release();
}

@Override
public void onResume() {
    super.onResume();
    mCamera.open();
}

Have you tried to call release() when you are being destroyed? 您是否曾尝试在销毁文件时调用release()?

From: http://developer.android.com/reference/android/hardware/Camera.html 来自: http : //developer.android.com/reference/android/hardware/Camera.html

Important: Call release() to release the camera for use by other applications. 重要提示:调用release()释放相机以供其他应用程序使用。 Applications should release the camera immediately in onPause() (and re-open() it in onResume()). 应用程序应立即在onPause()中释放相机(并在onResume()中重新打开)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 屏幕方向被强制设置为纵向,但倾斜时活动仍将旋转为横向! 如何强制人像模式? - Screen orientation forced to portrait, but still, the activities will rotate to landscape when tilted!! How to force portrait mode? 当我的应用程序处于纵向状态时,请使用横向相机 - Use camera in landscape when my app is in portrait 相机确认从风景变为肖像 - Camera confirmation changes to portrait from landscape 从“纵向”更改为“风景”以及从“风景”更改为“人像”时取消EditText的内容 - cancellation of the content of the EditText when changing from PORTRAIT to Landscape and landscape to portrait 当使用相机的速度太慢时,Android从横向移动到纵向(反之亦然)吗? - Android move from Landscape to Portrait (or vice versa) when using camera is too slow? 从纵向切换为横向时出错 - Error when switching from Portrait to Landscape 从横向/纵向旋转时,DatePickerDialog关闭 - DatePickerDialog closes when rotating from landscape/portrait 以人像模式从相机拍摄的加载到画布上的照片为横向 - Photos loaded onto canvas taken from a camera in portrait mode are landscape 纵向模式下的相机风景预览 - Camera landscape preview in portrait mode 为什么手机从垂直横向倾斜到水平横向时我的应用程序崩溃 - why my Application Crashes when Phone is Tilted from vertical to horizontal landscape mode
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM