我创建了一个Android服务,该服务使用画布在Window.manager上创建了一个圆圈,在触摸该圆圈时,我想打开/关闭手电筒。

我可以开启Flashlist,但再次触摸将其关闭时,应用程序将崩溃。

public void torch(int val){
    try {
        camera = Camera.open();
    } catch (Exception e) {
        Log.e("ERROR", "CameraInstance", e);
    }
    if(val == 1) {
        if (camera != null) {
            parameters = camera.getParameters();
            parameters.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
            camera.setParameters(parameters);
            camera.startPreview();
        }
    }
    else
    {
        if (camera != null){
            parameters = camera.getParameters();
            parameters.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
            camera.setParameters(parameters);
            camera.stopPreview();
        }
    }
}
}
else if(evtouchpossitiondownX >= XH && evtouchpossitiondownX < (XH + WH)
                                                  && evtouchpossitiondownY >= YH && evtouchpossitiondownY < (YH + HH)) {
                                              //Homescreen
                                             /* Intent startMain = new Intent(Intent.ACTION_MAIN);
                                              startMain.addCategory(Intent.CATEGORY_HOME);
                                              startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                                              startActivity(startMain);*/
                                              process.torch(1);

                                              windowManager.removeView(control_cirlce);
                                              windowManager.addView(floatingFaceBubble, myParams);


我正进入(状态


  java.lang.RuntimeException:无法连接到摄像头服务
              在android.hardware.Camera。(Camera.java:497)
              在android.hardware.Camera.open(Camera.java:357)
              在com.amlpl.laziapp.Bubdub.processor.torch(processor.java:123)
              at com.amlpl.laziapp.Bubdub.laziservice $ 3.onTouch(laziservice.java:255)
              在android.view.View.dispatchTouchEvent(View.java:8387)
              在android.view.View.dispatchPointerEvent(View.java:8583)
              在android.view.ViewRootImpl $ ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:4091)
              在android.view.ViewRootImpl $ ViewPostImeInputStage.onProcess(ViewRootImpl.java:3957)
              在android.view.ViewRootImpl $ InputStage.deliver(ViewRootImpl.java:3509)
              在android.view.ViewRootImpl $ InputStage.onDeliverToNext(ViewRootImpl.java:3562)
              在android.view.ViewRootImpl $ InputStage.forward(ViewRootImpl.java:3528)
              在android.view.ViewRootImpl $ AsyncInputStage.forward(ViewRootImpl.java:3638)
              在android.view.ViewRootImpl $ InputStage.apply(ViewRootImpl.java:3536)
              在android.view.ViewRootImpl $ AsyncInputStage.apply(ViewRootImpl.java:3695)
              在android.view.ViewRootImpl $ InputStage.deliver(ViewRootImpl.java:3509)
              在android.view.ViewRootImpl $ InputStage.onDeliverToNext(ViewRootImpl.java:3562)
              在android.view.ViewRootImpl $ InputStage.forward(ViewRootImpl.java:3528)
              在android.view.ViewRootImpl $ InputStage.apply(ViewRootImpl.java:3536)
              在android.view.ViewRootImpl $ InputStage.deliver(ViewRootImpl.java:3509)
              在android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:5806)
              在android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:5745)
              在android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:5716)
              在android.view.ViewRootImpl $ WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:5896)
              在android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:192)
              在android.os.MessageQueue.nativePollOnce(本地方法)
              在android.os.MessageQueue.next(MessageQueue.java:143)
              在android.os.Looper.loop(Looper.java:122)
              在android.app.ActivityThread.main(ActivityThread.java:5312)
              在java.lang.reflect.Method.invoke(本机方法)
              在java.lang.reflect.Method.invoke(Method.java:372)
              在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:901)
              在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696)

最佳答案

编辑:
也许尝试在调用open之前检查您的相机对象是否已初始化。可能在照相机已经打开时调用Camera.open();导致此错误。

if(camera == null){
    try {
        camera = Camera.open();
    } catch (Exception e) {
        Log.e("ERROR", "CameraInstance", e);
    }
}

07-27 15:54