4

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)

Mehmet K
  • 2,805
  • 1
  • 23
  • 35
Ruslan Leshchenko
  • 4,043
  • 4
  • 24
  • 36
  • I have the same issue on ZTE BLADE V7 and Sony Xperia M5 (Android 6.0). The issue comes from Crashlytics. I cannot reproduce it at the moment. My only idea for a workaround so far is to use old camera API for these phone models or even for all Android versions pre-7.0. – Slav Feb 08 '18 at 08:52
  • @Slav I posted the solution. – Ruslan Leshchenko Feb 08 '18 at 15:29

2 Answers2

1

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.

Ruslan Leshchenko
  • 4,043
  • 4
  • 24
  • 36
0

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();
    }
}