Android Turn on/off Camera Flash Programmatically with Camera2

android camera2 flashlight example
android turn on flashlight programmatically marshmallow
turn on flashlight programmatically android
how to blink flashlight in android programmatically
how to turn on front flash light
how to blink flashlight in android programmatically github
android camera and flashlight
the flash lamp or camera is occupied please turn it off before accessing the camera

I am making a simple application that acts as a flashlight. I need to be able to turn on and off the flash from a button in the application with the Camera2 API, as I get errors with the old camera API. I would like to use a "torch" flashlight, where the camera doesn't have to open in order for the flash to turn on. I know it's possible, as lots of phones have a flash on/off in the quick settings menu, but I can't find any code or tutorials about how to do this.


Camera cam =;     
Parameters p = cam.getParameters();

I am running on a Nexus 6P with Android 6.0.1.

EDIT: I need to use the Camera2 API. I haven't found a good tutorial for this yet, and after getting an answer, I will create one in Q/A format on here.

I'll be addressing you to the Android Dev. documentation about the CameraManager since more code will be required but this is the basic code to activate the Flash on the camera in API's above 21.

CameraManager camManager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);
String cameraId = camManager.getCameraIdList()[0]; // Usually front camera is at 0 position.
camManager.setTorchMode(cameraId, true);

Android CameraManager documentation.

Android.hardware.camera2 documentation.

very important thing to remember that you will need to use try/catch to discard possible errors and ofcourse check that no other higher priority application is using the camera at the moment.

How to turn on Flash light programmatically in android?, Step 1 − Create a new project in Android Studio, go to File ⇒ New Project android:text="Turn On/Off Camera LED/ Flashlight Android" /> </LinearLayout> PackageManager; import android.hardware.camera2. Open the “Camera” app. Tap the flash icon. Some models may require you to select the “Menu” icon ( or ) first. You may have to tap or swipe left to make the buttons appear. Toggle the lighting icon to the desired setting. Lightning with nothing = Flash will activate on every picture.

    mCameraManager = (CameraManager) context.getSystemService(Context.CAMERA_SERVICE);
    try {
        for (String camID : mCameraManager.getCameraIdList()) {
            CameraCharacteristics cameraCharacteristics = mCameraManager.getCameraCharacteristics(camID);
            int lensFacing = cameraCharacteristics.get(CameraCharacteristics.LENS_FACING);
            if (lensFacing == CameraCharacteristics.LENS_FACING_FRONT && cameraCharacteristics.get(CameraCharacteristics.FLASH_INFO_AVAILABLE)) {
                mCameraId = camID;
            } else if (lensFacing == CameraCharacteristics.LENS_FACING_BACK && cameraCharacteristics.get(CameraCharacteristics.FLASH_INFO_AVAILABLE)) {
                mCameraId = camID;
        if (mCameraId != null) {
            mCameraManager.setTorchMode(mCameraId, true);
    } catch (CameraAccessException e) {

mCameraId will preferably turn on front camera flash by priority and if there is no front flash then back camera flash will be used. If device has no flash then mCameraId will be null and setTorchMode will not be called in the code above.

Android Flash Light Application Tutorial Using Camera2 API, In this tutorial, we will explain how to create free flashlight app for android. the torch is currently on or off, then we call the turnOffFlashLight() to turn flash off in Similarly in the turnOnFlashLight() we turn on the flashlight programmatically by  In this tutorial we are simply turn on and turn off android mobile phone device camera flash light. So here is the complete step by step tutorial for Turn on only camera FlashLight programmatically in android.

Try this. Is working fine on me for android Pie and above. There is a toggle button that turns on and off the flashlight

    ToggleButton FlB = (ToggleButton) findViewById(;

    final boolean[] IsTurnedOn = {false};
    final CameraManager camManager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);
    String cameraId = null; // Usually front camera is at 0 position.
    try {
        cameraId = camManager.getCameraIdList()[0];
    } catch (CameraAccessException e) {
    final String finalCameraId = cameraId;

    FlB.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {

            if (!IsTurnedOn[0]){
                try {
                    camManager.setTorchMode(finalCameraId, true);
                } catch (CameraAccessException e) {
            else {
                try {
                    camManager.setTorchMode(finalCameraId, false);
                } catch (CameraAccessException e) {
            IsTurnedOn[0] =!IsTurnedOn[0];


How to turn on camera flash light programmatically in Android , Android Lollipop introduced camera2 API and deprecated the previous camera API. However, using the deprecated API to turn on the flash still works and is  Update 3 (Widget for turning on/off camera led) If you want to develop a widget that turns on/off your camera led, then you must refer my answer Widget for turning on/off camera flashlight in android.. Update 4

Simplest and most banal way to do a flashlight app

// xml code

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android=""


//java code

public class MainActivity extends AppCompatActivity {
    protected void onCreate(Bundle savedInstanceState) {

    Button flashButton;
    String buttonText;

    public void checkState(String buttonText) {

        switch (buttonText) {

            case "ON":



            case "OFF":



    public void turnOnOff(View view) {

        flashButton = (Button) findViewById(;
        buttonText = flashButton.getText().toString();


    public void flashOn() {

        Camera camera =;
        Camera.Parameters parameters = camera.getParameters();


    public void flashOff() {

        Camera camera2 =;
        Camera.Parameters parameters2 = camera2.getParameters();



// AndroidManifest

<?xml version="1.0" encoding="utf-8"?>

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


    <activity android:name=".MainActivity">
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />

CaptureRequest, package com.example.flashlight; import android.hardware.Camera; import Android Lollipop introduced camera2 API and deprecated the previous camera API. android - Turn ON/OFF Camera LED/flash light in Samsung Galaxy Ace 2.2. I need to use only the flash with the API camera2 (Android 5, API level 21), like torch applications. But all the examples that I found requires the display of camera stream in a view

Android Flash Light Application Tutorial Using Camera2 API , Contains the configuration for the capture hardware (sensor, lens, flash), the processing From class android.hardware.camera2. Allow camera device to enable zero-shutter-lag mode for requests with When set to true (ON), the values used for black-level compensation will not change until the lock is set to false (OFF). adsense Adding Permissions to Use Camera and FlashLight Open your AndroidManifest.xml file and add the following permissions. These uses-permissions tag tells the Android OS that our app will require access to CAMERA and FLASHLIGHT.

Android FlashLight Tutorial, After following this tutorial, you can create the best flashlight app for Android and turnOnFlashLight() to turn flash on in case the torch is currently off. we turn on the flashlight programmatically by setting mCameraManager. My code binding of Camera2Basic source. Work with API 21 and 22 on phones Sony Xperia M2 (5.1.1) and Sony Xperia M4 Aqua (5.0.2) : using Android.Hardware.Camera2;

Turn on and turn off flash light programmatically in Android – A , Android FlashLight Tutorial, Android Studio Flashlight ON-OFF, toggle, In the next part, we'll be using the Camera2 API to toggle the flashlight. Over that id, we can set the torch mode to true to enable the flashlight or to false to disable it. This example demonstrates how do I turn on Flash light programatically in android. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.

  • check out this link might help…
  • I've used that exact code :P. That was the first question I found and I get the error: An error occurred while connecting to camera: 0
  • error occured? are you sutre that you had all required <uses-permission>
  • Yes. Both permissions for camera and both for Android.hardware.
  • can you paste the code that you are using in here please so we might see if you used it correctly.
  • Is the rear camera (the one with the flash) in position 1?
  • im sry I was talking about the rear camera, but you can always test it and if you read the documentation you can also getCameraProperties and see if that specific camera have a Flash attached or not, small tip for coding, always read the documentation and play with it as practice.
  • Ok. I'll try to get the full list and edit it to my post. I'm testing your code now...
  • It works! I will be editing the original post with the full code to support Lollipop and older API levels. (For those who don't want to only support 5.0+ like me)
  • please mark this as an answer if that works, your welcome. :D
  • This is not the correct way to use the API, even though this will work on many devices. The API requires that you set a preview target before calling startPreview, and that is enforced on many devices (such as all newer Nexus phones).
  • It is incorrect answer! Question is about newer Camera2 API, but answer uses older camera API version.