9
Camera cam = Camera.open();     
Parameters p = cam.getParameters();
p.setFlashMode(Parameters.FLASH_MODE_TORCH);
cam.setParameters(p);
cam.startPreview();

The above dose not work on Lollipop, Because Camera is deprecated in Lollipop. I cant able to find any other way to turn on flash programmatically in Lollipop. How can I achieve this. Thanks in advance.

Egor Neliuba
  • 14,784
  • 7
  • 59
  • 77
Sai
  • 15,188
  • 20
  • 81
  • 121

3 Answers3

10

Camera class is now deprecated.

For LOLLIPOP above you need to use camera2 Api

so nickkadrov's solution doesent work for 6.0 & above device,best way to on/off flash light is try code below

public static void toggleFlashLight(){
    toggle=!toggle;
               try {
            CameraManager cameraManager = (CameraManager) getApplicationContext().getSystemService(Context.CAMERA_SERVICE);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                for (String id : cameraManager.getCameraIdList()) {

                    // Turn on the flash if camera has one
                    if (cameraManager.getCameraCharacteristics(id).get(CameraCharacteristics.FLASH_INFO_AVAILABLE)) {
                        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                            cameraManager.setTorchMode(id, true);
                        }
                    }
                }
            }
        } catch (Exception e2) {
            Toast.makeText(getApplicationContext(), "Torch Failed: " + e2.getMessage(), Toast.LENGTH_SHORT).show();
        }


}

where toggle is class level static Boolean variable whose default value is false

static boolean toggle=false;
The Black Horse
  • 2,328
  • 1
  • 14
  • 21
7
mCam = Camera.open();
Camera.Parameters p = mCam.getParameters();
p.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
mCam.setParameters(p);
mPreviewTexture = new SurfaceTexture(0);
try {
   mCam.setPreviewTexture(mPreviewTexture);
} catch (IOException ex) {
   // Ignore
}
mCam.startPreview();

It works for me on Android 5.0.x. And don't forget to add permission in manifest for camera usage.

<uses-permission android:name="android.permission.CAMERA" />
Eddy Talvala
  • 17,243
  • 2
  • 42
  • 47
nickkadrov
  • 488
  • 2
  • 12
1

Your code should actually work. Please check if you added the permission for using the camera properly:

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

This should be added to your AndroidManifest above of your other specifications.

Additionally, there is an interesting discussion about different devices and an example which should work on every device here: Flashlight in Android

If you dont want to use the deprecated API, you can check out:

Package Summary of Camera2

Camera device specification on the new api

Unfortunately I can not give you an example for using the new API, because I did not use it myself yet.

Community
  • 1
  • 1
Matthias
  • 158
  • 1
  • 11