Turn on and turn off flash light programmatically in Android

Most of times we need to use flash light of our Android device as torch so we have to start camera or other application. But Android provide access to the camera flash in our application so you can turn it on and off using code. Here I’ll show you how to turn on and off it programmatically.

First of all we need to check if the device has the flash light or not. For that we use following condition from PackageManager.

Here is the function for turn on the flash light.

Camera cam = null;
public void turnOnFlashLight() {
    try {
        if (getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH)) {
            cam = Camera.open();
            Parameters p = cam.getParameters();
    } catch (Exception e) {
        Toast.makeText(getBaseContext(), "Exception throws in turning on flashlight.", Toast.LENGTH_SHORT).show();

And to turn of flashlight use following piece of code.

public void turnOffFlashLight() {
    try {
        if (getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH)) {
            cam = null;
    } catch (Exception e) {
        Toast.makeText(getBaseContext(), "Exception throws in turning off flashlight.", Toast.LENGTH_SHORT).show();

Here we use Camera class for flashlight.

Here we need to add the following permission to the manifest file. The last one is not generally used in 3rd party apps, it is just use for testing in system apps as per docs.

<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.FLASHLIGHT"/>
<uses-permission android:name="android.permission.HARDWARE_TEST" />

Here you can download full code from github: Flashligh Demo

You can download demo app from Google Play: Flashlight Demo


6 thoughts on “Turn on and turn off flash light programmatically in Android

  1. Not working nexus 5

  2. No errors on N5, it simply does nothing.

    • Can you please post your logcat output so I can get some idea. I think N5 with kitkat requires an additional permission.

      • Hi, Same issue with Nexus 5 with my app.
        Nothing happens in Logcat, it’s just not flashing, The code is executed, I put different logs inside to verify.
        I have the right permissions in my manifest, it works on any machine except Nexus 5. It seems to be a question of SurfaceView the camera should be linked with to have the flash operate, even if we don’t need to preview anything. Have you heard about that? No good resource in StackOverflow or whatever.
        If you have an answer, it would be great,

  3. Pingback: Download Flashlight Demo 1.0 for android

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s