title: Flash toggle button crash android
body: <blockquote>
    <p>App is working fine in four different devices. but client is facing crash on flash buttton ON/OFF on <strong>Xperia z2</strong>.</p>
  </blockquote>
  
  <p><strong>MainActivity</strong></p>
  
  <pre><code>btnFlash.setOnClickListener(new OnClickListener()
          {
              @Override
              public void onClick(View arg0)
              {
                  Toast.makeText(context, "Flash is toggled", Toast.LENGTH_LONG).show();
  
                  if(CameraPreview.isFlashOn)
                  {
                      CameraPreview.FlashOff(camera);
  
                      CameraPreview.isFlashOn = false;
                  }
                  else
                  {
                      CameraPreview.isFlashOn = true;
  
                      CameraPreview.FlashOn(camera);
                  }
              }
          });
  </code></pre>
  
  <p><strong>CameraPreview</strong></p>
  
  <pre><code>  public static boolean isFlashOn =false; // 
  
    public static void FlashOn(Camera mCamera)
    {
          // Get Camera Params for customisation
          Camera.Parameters parameters = mCamera.getParameters();
  
          // Check Whether device supports AutoFlash, If you YES then set
          // AutoFlash
          List&lt;String&gt; flashModes = parameters.getSupportedFlashModes();
  
          if (flashModes.contains(android.hardware.Camera.Parameters.FLASH_MODE_TORCH))
          {
              parameters.setFlashMode(Parameters.FLASH_MODE_TORCH);
          }
          else if (flashModes.contains(Parameters.FLASH_MODE_ON))
          {
              parameters.setFlashMode(Parameters.FLASH_MODE_ON);
          }
          mCamera.setParameters(parameters);
          mCamera.startPreview();
      }
  
      public static void FlashOff(Camera mCamera)
      {
          // Get Camera Params for customisation
          Camera.Parameters parameters = mCamera.getParameters();
  
          // Check Whether device supports AutoFlash, If you YES then set
          // AutoFlash
          List&lt;String&gt; flashModes = parameters.getSupportedFlashModes();
          if (flashModes.contains(android.hardware.Camera.Parameters.FLASH_MODE_OFF))
          {
              parameters.setFlashMode(Parameters.FLASH_MODE_OFF);
          }
          mCamera.setParameters(parameters);
          mCamera.startPreview();
      }
  </code></pre>
  
  <p><strong>Crash Report.</strong></p>
  
  <pre><code>me_idx 0, stream type 9
  W/ActivityManager(  973): Activity pause timeout for ActivityRecord{45328828 u0
  aus.xray.cam/.MainActivity2 t153 f}
  D/mm-camera-intf( 9138): mm_stream_read_msm_frame: VIDIOC_DQBUF buf_index 6, fra
  me_idx 840, stream type 1
  I/mm-camera(  473): mct_pipeline_process_set:command=8000013
  I/mm-camera(  473): pproc_module_handle_reprocess_offline:1318 input buf fd 126
  type 4 size 462848
  D/mm-camera-intf( 9138): mm_stream_read_msm_frame: VIDIOC_DQBUF buf_index 0, fra
  me_idx 0, stream type 9
  D/CustomizationProcess( 3778): dismissCustomizationDialog:true
  I/WindowManager(  973): Screen frozen for +307ms due to Window{44e24b48 u0 Statu
  sBar}
  D/mm-camera-intf( 9138): mm_stream_read_msm_frame: VIDIOC_DQBUF buf_index 0, fra
  me_idx 841, stream type 1
  I/mm-camera(  473): mct_pipeline_process_set:command=8000013
  I/mm-camera(  473): pproc_module_handle_reprocess_offline:1318 input buf fd 120
  type 4 size 462848
  D/mm-camera-intf( 9138): mm_stream_read_msm_frame: VIDIOC_DQBUF buf_index 1, fra
  me_idx 0, stream type 9
  ^C
  C:\adb&gt;
  </code></pre>