androidandroid-cameraflashlight

How to check if the device has a flash for front and back facing camera separately?


I am currently working on a camera activity. I managed to access my device's back camera flash light and to hide the flash toggle button when I switch to the front camera automatically. However, I was wondering if there was a way to check for secondary flash lights since many smartphone models come with front camera flash light and that would also help when using this application from a tablet without back camera flash light. My idea is checking for the front and back facing camera flash lights separately with two independent booleans and if the flash light is not available, setting the toggle button invisible. I really dislike the idea of showing or hiding the flash button without making sure the device has flash light or not in any of its cameras. This is what I have so far. Any ideas?

private boolean hasFlash(Context Context) {
    if (Context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH)) {
        return true;
    } else {
        return false;
    }
}

_

 if (!hasFlash(Context)) {
            ImageButton FlashButton = (ImageButton) findViewById(R.id.frnxcameraflashbutton);
                            FlashButton.setVisibility(View.INVISIBLE);
                            FlashButton.setImageResource(R.mipmap.cameraflashoffbutton);
        }

Solution

  • If you are able to use the new Camera2 APIs (mostly 21+ I think), the CameraCharacteristics key-value map available for each camera should indicate whether or not each camera has a corresponding flash. For example, you could probably just check for the FLASH_STATE_UNAVAILABLE flag per-camera to achieve your goal.