I just started to develop an Android app that needs to use camera features in order to capture images and I want to keep the Flash ALWAYS ON. The problem is that when I start the camera from app the flash only blinks once and stops.
I implemented the camera with Camera class and I used the CameraPreview that extends SurfaceView and implements SurfaceHolder exactly like the example from android developer page: Here
Then I used a FrameLayout to show the camera preview:
// Create an instance of Camera
mCamera = getCameraInstance();
Parameters p = mCamera.getParameters();
p.setFlashMode(Parameters.FLASH_MODE_TORCH);
mCamera.setParameters(p);
// Create our Preview view and set it as the content of our activity.
mPreview = new CameraPreview(this, mCamera);
FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview);
preview.addView(mPreview);
Everything works good excepting the flash that blinks once and stops. I tested the app on a Samsung Galaxy S5 with KitKat.
Intent for camera don't fit my needs and Camera2 is excluded.
Thanks!
Found the solution:
public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
// If your preview can change or rotate, take care of those events here.
// Make sure to stop the preview before resizing or reformatting it.
if (mHolder.getSurface() == null){
// preview surface does not exist
return;
}
// stop preview before making changes
try {
mCamera.stopPreview();
} catch (Exception e){
// ignore: tried to stop a non-existent preview
}
// set preview size and make any resize, rotate or
// reformatting changes here
// start preview with new settings
try {
//Turn flash on
Camera.Parameters p = mCamera.getParameters();
p.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
mCamera.setParameters(p);
mCamera.setPreviewDisplay(mHolder);
mCamera.startPreview();
} catch (Exception e){
Log.d(TAG, "Error starting camera preview: " + e.getMessage());
}
}
Add the code to start the flash in the second try-catch of surfaceChanged method.