无法连接到Android中的相机服务

时间:2012-07-25 10:13:59

标签: android android-camera

在从另一个活动导航到摄像机活动时出现异常“无法连接到摄像头服务”。问题发生在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;
        }

    }
}

添加Log cat

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)

2 个答案:

答案 0 :(得分:4)

拒绝了CameraService :: connect X(pid 623)(摄像头0仍然忙碌)。

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

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

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

答案 1 :(得分:0)

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

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

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