2

从另一个活动导航到相机活动时出现异常“无法连接到相机服务”。问题发生在 Camera.open()。我搜索了很多并尝试了可用的解决方案。但问题没有解决。请提供一些帮助。在此先感谢。

 @Override
    public void surfaceChanged(final SurfaceHolder holder, int arg1,
            int arg2, int arg3) {


        cameraHandler = new Handler() {
            /*
             * (non-Javadoc)
             * 
             * @see android.os.Handler#handleMessage(android.os.Message)
             */
            @Override
            public void handleMessage(Message msg) {
                switch (msg.what) {
                case LOADCAMERA:
                    if (null != mProgress) {
                        if (mProgress.isShowing()) {
                            mProgress.dismiss();
                        }
                    }
                    try {
                        try {

                            mCam = Camera.open(); // attempt to get a Camera
                                                    // instance
                        } catch (Exception e) {
                            Log.e("Exception", e.getMessage());
                        }
                        mCam.setPreviewDisplay(holder);
                        mCameraParameters = mCam.getParameters();
                        if (isFlashLightOn) {
                            mCameraParameters
                                    .setFlashMode(Parameters.FLASH_MODE_TORCH);
                        } else {
                            mCameraParameters
                                    .setFlashMode(Parameters.FLASH_MODE_OFF);
                        }
                        mCameraParameters.setPreviewSize(mVideoWidth,
                                mVideoHeight);
                        mCam.setParameters(mCameraParameters);
                        mCam.startPreview();
                        mCam.unlock();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }

                }
                super.handleMessage(msg);
            }
        };
        Message msg = new Message();
        msg.what = LOADCAMERA;
        cameraHandler.sendMessageDelayed(msg, 700);

    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {

    }

    @Override
    public void surfaceDestroyed(SurfaceHolder arg0) {


        if (mCam != null) {
            mCam.stopPreview();
            mCam.setPreviewCallback(null);
            mCam.release();
            mCam = null;
        }

    }
}

添加了日志猫

W/CameraService(   78): CameraService::connect X (pid 623) rejected (camera 0 is still      busy).
E/Exception(  623): Fail to connect to camera service
D/AndroidRuntime(  623): Shutting down VM
W/dalvikvm(  623): threadid=1: thread exiting with uncaught exception(group=0x40015560)
E/AndroidRuntime(  623): FATAL EXCEPTION: main
E/AndroidRuntime(  623): java.lang.NullPointerException
E/AndroidRuntime(  623):    at   com.gui.MainActivity$CameraPreview$1.handleMessage(MainActivity.java:2041)
E/AndroidRuntime(  623):    at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime(  623):    at android.os.Looper.loop(Looper.java:130)
E/AndroidRuntime(  623):    at  android.app.ActivityThread.main(ActivityThread.java:3683)
E/AndroidRuntime(  623):    at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(  623):    at java.lang.reflect.Method.invoke(Method.java:507)
E/AndroidRuntime(  623):    at    com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:845)
E/AndroidRuntime(  623):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:603)
E/AndroidRuntime(  623):    at dalvik.system.NativeStart.main(Native Method)
W/ActivityManager(  106):   Force finishing activity com.gui/.MainActivity
W/ActivityManager(  106):   Force finishing activity com.gui/.MainActivity
E/v4l2_utils(   78): Error = Try again from dqbuf
E/SEC_Overlay(   78): Failed to DQ/-1
E/CameraHardwareSec(   78): ERR(int    android::CameraHardwareSec::previewThread()):overlay dequeueBuffer fail
I/CameraHardwareSec(   78): int android::CameraHardwareSec::previewThreadWrapper():  calling mSecCamera->stopPreview() and waiting
I/CameraHardwareSec(   78): int android::CameraHardwareSec::previewThreadWrapper(): return from wait
I/CameraHardwareSec(   78): int android::CameraHardwareSec::previewThreadWrapper(): exiting
W/SecCamera(   78): int android::SecCamera::stopPreview(): doing nothing because m_flag_camera_start is zero
I/CameraHardwareSec(   78): virtual void android::CameraHardwareSec::release(): calling mPreviewHeap.dispose()
W/SecCamera(   78): int android::SecCamera::stopRecord(): doing nothing because     m_flag_record_start is zero
I/SecCamera(   78): DeinitCamera: m_cam_fd(15)
I/SecCamera(   78): DeinitCamera: m_cam_fd2(21)
4

2 回答 2

4

CameraService::connect X (pid 623) 被拒绝(摄像机 0 仍然忙)。

这意味着其他一些进程(App)已经调用了“Camera.open”并且没有释放它。在它释放之前,您无法访问相机。

从另一个活动导航到相机活动时,“出现异常”无法连接到相机服务“是什么意思。

这是什么其他活动,它是否访问相机?

于 2012-07-25T23:39:08.303 回答
0

也许,在您的 MainActivity 中,您可以添加:

protected override void OnDestroyed() {
    if (mCam != null) {
        mCam.release();
    }
}

我认为您的错误可能是因为相机在退出之前仍与应用程序保持开放连接。至少,这就是解决我的问题的原因。

于 2020-05-28T08:21:51.773 回答