Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to check if device has flash light led android

How can I check if a device has a camera led (flashlight)? I am talking about devices with android OS?

I have seen solutions some solutions which talks about how to turn the led on and off but what will happen if the device doesn't even has a led.

for turning on the camera I am using camera.open()

like image 293
vlio20 Avatar asked Nov 16 '12 09:11

vlio20


People also ask

How do I know if my phone has Flash?

Adobe Flash Player needs to be installed in order to view Flash-based software on an Android phone or tablet. You can either install Adobe Flash and the Firefox browser, or install the FlashFox browser which has Flash Player embedded. From the Play Store, install FlashFox.

What is mobile flash light?

You can turn on the flashlight on most Androids by pulling down the Quick Settings menu from the top of the screen and tapping the flashlight button. You can also turn on the flashlight with a voice command to Google Assistant . Some Android phones also let you turn on the flashlight with a gesture or a shake.

Why isn't my flash working on my Android?

Restart the phone If a particular app or process is conflicting with the flashlight, then a simple reboot should fix it. Just hold the power button and select “Power off” from the menu. Now wait 10-15 seconds and turn it back on. This should fix the problem in most cases.

Why my mobile Flash is not working?

Clear the camera app data to return your camera to its default settings and make flashlight work To clear camera app data; Go to SETTINGS >>> APPLICATION MANAGER >>> ALL >>> CAMERA >>> CLEAR DATA. You can also use another flashlight app if your default flashlight switch is not working.


5 Answers

The other answers

boolean hasFlash = this.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);

Does not work for the new 2013 Nexus 7. The following code will work:

public boolean hasFlash() {
        if (camera == null) {
            return false;
        }

        Camera.Parameters parameters;
        try {
            parameters = camera.getParameters();
        } catch (RuntimeException ignored)  {
            return false;
        }

        if (parameters.getFlashMode() == null) {
            return false;
        }

        List<String> supportedFlashModes = parameters.getSupportedFlashModes();
        if (supportedFlashModes == null || supportedFlashModes.isEmpty() || supportedFlashModes.size() == 1 && supportedFlashModes.get(0).equals(Camera.Parameters.FLASH_MODE_OFF)) {
            return false;
        }

        return true;
    }
like image 123
Erik B Avatar answered Oct 20 '22 12:10

Erik B


getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH) returns true if the device has flash. See this for more details

like image 41
PC. Avatar answered Oct 20 '22 12:10

PC.


You should be able to check whether the flash is available by checking system features:

boolean hasFlash = this.getPackageManager()
                       .hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);

(provided you are in an Activity). If not, than use some sort of context in place of this.

P.S. Note that this information is quite easy to find if you actually try searching for it.

like image 32
Aleks G Avatar answered Oct 20 '22 14:10

Aleks G


PackageManager pm = context.getPackageManager();
        if (!pm.hasSystemFeature(PackageManager.FEATURE_CAMERA)) {
            Log.e("err", "Device has no camera!");
            return;
        }       
        camera = Camera.open();
        p = camera.getParameters();
        flashModes = p.getSupportedFlashModes();
if(flashModes==null){
                        Toast.makeText(getApplicationContext(), "LED Not Available",Toast.LENGTH_LONG).show();
                }else
                {
Toast.makeText(getApplicationContext(), "LED  Available",Toast.LENGTH_LONG).show();
}
like image 30
appukrb Avatar answered Oct 20 '22 14:10

appukrb


This is how I check if LED flash is available. Also you don't need to have the Camera permission to run this method.

private fun isLedFlashAvailable(context: Context): Boolean {
    // method 1
    if (context.packageManager.hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH)) {
        return true
    }

    // method 2
    val cameraManager = context.getSystemService(Context.CAMERA_SERVICE) as CameraManager
    for (id in cameraManager.cameraIdList) {
        if (cameraManager.getCameraCharacteristics(id).get(CameraCharacteristics.FLASH_INFO_AVAILABLE) == true) {
            return true
        }
    }

    return false
}
like image 40
Egis Avatar answered Oct 20 '22 13:10

Egis