When trying turn off camera led, app crash

2019-07-03 22:30发布

问题:

i have a code

 @TargetApi(23)
 private void setTorchMode( CameraManager cameraManager, String id, boolean switchOn) throws CameraAccessException {


      try {
            final Method setTorchMode = cameraManager.getClass().getMethod("setTorchMode", String.class, boolean.class);
            cameraManager.setTorchMode(cameraManager.getCameraIdList()[0],switchOn);
            callbackContext.success();
      } catch (IllegalArgumentException m){

      } catch (Throwable t) {
            callbackContext.error(t.getMessage());
      }

when i try turn off camera led, i have crash

FATAL EXCEPTION: main
Process: uk.co.sparkenergy.androidapp2, PID: 31200    java.lang.IllegalArgumentException: Receiver not registered: android.hardware.camera2.CameraManager$1@6e1ab65
                                                                               at android.app.LoadedApk.forgetReceiverDispatcher(LoadedApk.java:793)
                                                                               at android.app.ContextImpl.unregisterReceiver(ContextImpl.java:1200)
                                                                               at android.hardware.camera2.CameraManager$3.run(CameraManager.java:1266)
                                                                               at android.os.Handler.handleCallback(Handler.java:815)
                                                                               at android.os.Handler.dispatchMessage(Handler.java:104)
                                                                               at android.os.Looper.loop(Looper.java:207)
                                                                               at android.app.ActivityThread.main(ActivityThread.java:5763)
                                                                               at java.lang.reflect.Method.invoke(Native Method)
                                                                               at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
                                                                               at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679)

only 1 device Sony e5633, help please)

回答1:

public void turnOnFlashLight() {

    try {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            mCameraManager.setTorchMode(mCameraId, true);

            mTorchOnOffButton.setImageResource(R.drawable.on);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}


public void turnOffFlashLight() {

    try {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            mCameraManager.setTorchMode(mCameraId, false);

            mTorchOnOffButton.setImageResource(R.drawable.off);

        }

    } catch (Exception e) {
        e.printStackTrace();
    }
}


回答2:

On some devices, if a flashlight is disabled and you try to disable it again, an app is crashed. So the solution is only to switch the state of the flashlight.