Flash 切换按钮崩溃 android

Flash toggle button crash android

App is working fine in four different devices. but client is facing crash on flash buttton ON/OFF on Xperia z2.

MainActivity

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

相机预览

  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<String> 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<String> flashModes = parameters.getSupportedFlashModes();
        if (flashModes.contains(android.hardware.Camera.Parameters.FLASH_MODE_OFF))
        {
            parameters.setFlashMode(Parameters.FLASH_MODE_OFF);
        }
        mCamera.setParameters(parameters);
        mCamera.startPreview();
    }

崩溃报告。

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>

应muku的要求。我自己回答了。用于切换闪光灯

flashBtn.setOnClickListener(new OnClickListener()
{
    @Override
    public onClick(View view)
    {
        if(cameraView!=null)//cameraView is the CameraPreview.class
        {
            if(cameraView.isFlashOn() && isFlashRunning)
            {       
                cameraView.FlashOff(camera);

                isFlashRunning = false;                             
            }
            else
            {
                cameraView.FlashOn(camera); 

                isFlashRunning = true;  
            }
    }
});

而FlashOn和FlashOff方法是

public void FlashOn(Camera mCamera) throws RemoteException
    {   
        // Get Camera Params for customisation
        Camera.Parameters parameters = mCamera.getParameters();

        // Check Whether device supports AutoFlash, If you YES then set
        // AutoFlash
        List<String> flashModes = parameters.getSupportedFlashModes();

        if (flashModes.contains(android.hardware.Camera.Parameters.FLASH_MODE_TORCH))
        {
            parameters.setFlashMode(Parameters.FLASH_MODE_TORCH);

            isFlashOn = true;
        }
        else if (flashModes.contains(Parameters.FLASH_MODE_ON))
        {
            parameters.setFlashMode(Parameters.FLASH_MODE_ON);

            isFlashOn = true;
        }

        mCamera.setParameters(parameters);

        mCamera.startPreview();

        //mCamera.autoFocus(null);  // may be this is problem
    }



    public void FlashOff(Camera mCamera) throws RemoteException
    {
        // Get Camera Params for customisation
        Camera.Parameters parameters = mCamera.getParameters();

        // Check Whether device supports AutoFlash, If you YES then set
        // AutoFlash
        List<String> flashModes = parameters.getSupportedFlashModes();

        if (flashModes.contains(android.hardware.Camera.Parameters.FLASH_MODE_OFF))
        {
            parameters.setFlashMode(Parameters.FLASH_MODE_OFF);

            isFlashOn = false;
        }

        mCamera.setParameters(parameters);

        mCamera.startPreview();
    }