Flash toggle button crash android

后端 未结 1 637
名媛妹妹
名媛妹妹 2021-01-25 03:24

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

Mai

相关标签:
1条回答
  • 2021-01-25 03:32

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

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

    And FlashOn and FlashOff methods are

    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();
        }
    
    0 讨论(0)
提交回复
热议问题