Question

I just started programming for Android after being a long time Java developer and I've created a little flashlight app (like there's not enough already xD). I realize that there are so many different phones and I've read its sometimes hard to get the LED light to consistently turn on on all devices as some require different approaches.

I currently use this approach to turn the camera LED on:

camera = Camera.open();

Camera.Parameters params = camera.getParameters();
params.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
camera.setParameters(params);
camera.setPreviewDisplay(holder);
camera.startPreview();

Obviously I can't really use an emulator to make sure the flash works so I've only been able to test it on a Samsung Galaxy Nexus and Nexus 4. Both of which it works on however I was wondering if this is the most compatible approach or is there something I should do to make it work for more devices?

Also a side note I've published the app free to Google Play, here, if you want to try it and let me know if it works on your device :)

Thank you for your help!

Was it helpful?

Solution

I just did one for fun myself. I realized that if you do setPreviewDisplay(holder) in GingerBeard device, it will not work. So I did something like this.

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
            camera.setPreviewDisplay(mSurfaceView.getHolder());
        }

OTHER TIPS

First check device is support flash light or not below code:

   boolean hasFlash = SevenBitsDemo.getInstance().getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);
   if (hasFlash) {
     // device support flash light:
     Camera.Parameters  cameraParams = mCamera.getParameters();
     cameraParams.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
   }else{
    // device is not support flash light 
   }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top