Turning on a flashlight on Android? Most compatible approach?

I just started Android programming after a long time Java developer and I created a small flashlight app (like this is not enough xD anymore). I realize that there are so many different phones out there and I have read sometimes that the LED indicator is constantly on on all devices as some require different approaches.

I am currently using this approach to turn on the camera LED:

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 use an emulator to make sure the flash works, so I was only able to test it on the Samsung Galaxy Nexus and Nexus 4. Both of them work, however I was wondering if the most compatible approach is or is there something that should i make it work for more devices?

Also note that I have published the app for free on Google Play, here if you want to try it out and let me know if it works on your device :)

Thanks for your help!

+3


source to share


2 answers


I just did it for fun. I figured out that if you use setPreviewDisplay (holder) in the GingerBeard device it won't work. So I did something like this.



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

      

+1


source


The first control device is flash support or 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 
   }

      

0


source







All Articles