Skip to content Skip to sidebar Skip to footer

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

Solution 1:

At the request of muku. I answered it my self. for toggling flash

flashBtn.setOnClickListener(newOnClickListener()
{
    @OverridepubliconClick(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;  
            }
    }
});

And FlashOn and FlashOff methods are

publicvoidFlashOn(Camera mCamera)throws RemoteException
    {   
        // Get Camera Params for customisation
        Camera.Parametersparameters= 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;
        }
        elseif (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
    }



    publicvoidFlashOff(Camera mCamera)throws RemoteException
    {
        // Get Camera Params for customisation
        Camera.Parametersparameters= 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();
    }

Post a Comment for "Flash Toggle Button Crash Android"