Android Turn on Flashlight Across ALL Versions of Android? - android

I've been working on an app that needs to turn the flashlight on and off on an Android phone. I need a way to turn on the flashlight across ALL versions of Android above API 15. The problem is, on older phones (API < 21), I have to use the traditional way (below). But on newer phones, like my Nexus 6P, I have to use the Camera2 interface (below the below). Is there one way to have the flashlight torch work on ALL versions of Android with one set of code?
Below:
camera = Camera.open();
Camera.Parameters parameters = camera.getParameters();
parameters.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
camera.setParameters(parameters);
camera.startPreview();
camera = Camera.open();
Camera.Parameters parameters = camera.getParameters();
parameters.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
camera.setParameters(parameters);
camera.stopPreview();
// camera.release(); used later when app closes
Below the Below:
CameraManager cameraManager = (CameraManager) context.getSystemService(Context.CAMERA_SERVICE);
String cameraId = cameraManager.getCameraIdList()[0];
cameraManager.setTorchMode(cameraId, state); // state is either true or false

A possible solution is to segment different sdk versions during runtime to determine which method to choose:
private final int sdkVersion = Build.VERSION.SDK_INT;
if (sdkVersion < Build.VERSION_CODES.LOLLIPOP) {
//do old way
} else {
//do new way
}
More information here: How to support multiple android version in your code?

Related

Controlling Flashlight in All Android devices

I'm using Camera Package for developing android app to turn on flash light, like this:
Camera mCamera = Camera.open();
List<String> flashModes = mCamera.getParameters().getSupportedFlashModes();
if(flashModes != null && flashModes.contains(Parameters.FLASH_MODE_TORCH)){
parameters.setFlashMode(Parameters.FLASH_MODE_TORCH);
mCamera.setParameters(parameters);
} else if (flashModes != null && flashModes.contains(Parameters.FLASH_MODE_ON)){
parameters.setFlashMode(Parameters.FLASH_MODE_ON);
mCamera.setParameters(parameters);
}
if(!previewing){
mCamera.startPreview();
previewing = true;
}
It check supporting camera flash modes, and adjust it. It works in Samsung Android devices, and some others, but doesn't works in LG V20, LG V30 and Mi Android. I searched for this and tried:
mCamera.autoFocus(Camera.AutoFocusCallback)
mCamera.setPreviewTexture(SurfaceTexture)
But nothing works. So I used Camera2 package for api>23, like:
CameraManager mCameraManager = (CameraManager) context.getSystemService(Context.CAMERA_SERVICE);
mCameraManager.setTorchMode("0", true);
And it works, but in Samsung device, an notification show up "Flashlight turned on" and it's very annoying.
So, I need the way to turn on flashlight for all devices with Camera package, or not to show up "Flashlight turned on" notification using Camera2 package.
I look forward to your reply, thanks.
The answer in below link saves my life!
The SurfaceView is essential for some devices.
LED flashlight on Galaxy Nexus controllable by what API?

NFC unavailable when camera is open

This is my Torch app:
final Camera.Parameters p;
Camera camera=Camera.open();
camera.setPreviewTexture(new SurfaceTexture(0));
p = camera.getParameters();
p.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
camera.setParameters(p);
camera.startPreview();
When my app is running, some devices cannot detect NFC tags. I noticed this happens with the Nexus 5X, specifically.
It appears certain devices can't detect NFC when the camera is running.
Can this problem be solved programmatically?
Unfortunately, no, but I sincerely hope Nexus 5X is the only device on which you will ever encounter this.
The reason is that pretty late in the development cycle of the 5X, it was found that the NFC controller polling introduced noise in the camera sensor. The only feasible fix at that time was to turn off NFC when the camera is opened :(
To solve this problem you can add this little code to your onStop, in the activity that uses the camera. If you need NFC, in some devices you need to release the camera.
#Override
protected void onStop() {
super.onStop();
try
{
android.hardware.Camera mCamera = android.hardware.Camera.open();
mCamera.release();
mCamera = null;
}
catch(RuntimeException e)
{
Log.e(TAG, "init_camera: " + e);
return;
}
}

Android camera api SCENE_MODE_HDR not supported in Nexus 5?

My Nexus 5 isn't supporting the HDR scene mode of the camera api (as well as the camera2 api). Is this due to the manufacturer support? If so, what I want to implement HDR scene mode in a custom camera app as in the stock camera?
I tried using both the camera APIs but none was supporting the SCENE_MODE_HDR parameter.
Using the android.hardware.camera api: (Logs HDR mode not supported)
List<String> sceneModes = params.getSupportedSceneModes();
if (sceneModes.contains(Camera.Parameters.SCENE_MODE_HDR)) {
Log.d("HDR", "HDR mode supported");
params.setSceneMode(Camera.Parameters.SCENE_MODE_HDR);
} else {
Log.d("HDR", "HDR mode not supported");
}
And using the android.hardware.camera2 api: (Logs HDR mode not supported)
CameraCharacteristics characteristics = manager.getCameraCharacteristics(cameraId);
int[] sceneModes= characteristics.get(CameraCharacteristics.CONTROL_AVAILABLE_SCENE_MODES);
boolean isHDRsupported = false;
for (int sceneMode : sceneModes) {
if (sceneMode == CameraCharacteristics.CONTROL_SCENE_MODE_HDR) {
isHDRsupported = true;
break;
}
}
Log.d("HDR", "HDR mode " + (isHDRsupported ? "" : "not ") + "supported");
Am I missing something obvious here?
The Nexus 5 does not have support for an HDR scene mode.
The HDR+ mode in the included camera app is part of the application itself (there's a blog post about how it works on top of the camera2 API).
Did you try?
Camera.Parameters cameraParameters = camera.getParameters();
cameraParameters.setSceneMode(Camera.Parameters.SCENE_MODE_HDR);
camera.setParameters(cameraParameters);

How to turn ON and OFF Device flash light?

I want to turn on only camera flash light (not with camera preview) programmatically in Android. I googled for it but the help I found was not working on my Samsung Galaxy ACE.
By using this code able to turn on for few seconds after that it goes to off automatically with out doing anything on the UI. If I try to turn on again getting force close.
private void turnOn() {
clicked = true;
mCamera = Camera.open();
Parameters params = mCamera.getParameters();
params.setFlashMode(Parameters.FLASH_MODE_ON);
mCamera.setParameters(params);
mCamera.startPreview();
mCamera.autoFocus(new AutoFocusCallback() {
public void onAutoFocus(boolean success, Camera camera) {
}
});
}
For turn OFF
private void turnOff() {
clicked = false;
if (mCamera != null) {
mCamera.stopPreview();
mCamera.release();
}
}
Declared these manifest permissions
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.FLASHLIGHT" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />
<uses-feature android:name="android.hardware.camera.flash" />
Can any body tell me How to turn on the flash light for a long time upto the use click on OFF button. Is it requires any third party libraries. I saw this app in the Google play Tiny flash how they did this app..
From my after turn on it goes to off in 2 sec I did not get what's the problem in this.
Try this
//Turn on
camera = Camera.open();
Parameters p = camera.getParameters();
p.setFlashMode(Parameters.FLASH_MODE_TORCH);
camera.setParameters(p);
camera.startPreview();
//Turn off
camera = Camera.open();
Parameters p = camera.getParameters();
p.setFlashMode(Parameters.FLASH_MODE_OFF);
camera.setParameters(p);
camera.stopPreview();
#RajaReddy PolamReddy,
I am using Samsung S3 LTE (4G). All you need change is to change Parameters.FLASH_MODE_ON to Parameters.FLASH_MODE_TORCH.
It works for me.
Bad news:
According to documentation android camera needs intialized surface view to start preview
Good news:
it must not be full screen or visible
My expirience so far that starting preview is necessary to activate flash ( but I can not speak for all devices )
If you need some examples how to activate preview and possibly hide it behind overlay,
look into javaocr library (see demos - there are 2 android apps, and also camera management library in separate project - you can just grab it)
https://sourceforge.net/projects/javaocr/

Setting Parameters.FLASH_MODE_TORCH doesn't work on Droid X 2.3

I am writing an app that sets the flash mode to torch. I have been testing the application on my Droid X, and the LED light does not come on. I tried it on a Droid Incredible and it worked fine. I can't figure out what the problem is. Here is part of my code for turning on torch mode.
Camera mCamera = Camera.open();
Camera.Parameters params = mCamera.getParameters();
if(params.getFlashMode() != null){
params.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
}
mCamera.setParameters(params);
I have added mCamera.startPreview(); because I read that should make a difference, but it doesn't. I also made a list of available flash modes and displayed them to the screen to make sure that my Droid X does have torch mode, and it was in the list. I even created a new application from code I found online that turns the LED flash on and off with a button. Again it worked fine on the Droid Incredible but not the Droid X. Is there something I am missing to get this to run on the Droid X, or could it be something with Gingerbread? The Droid X is running Gingerbread and the Droid Incredible is running FroYo.
There are quite a few quirks when setting FLASH_MODE_TORCH.
Often you need to start a camera preview:
Camera mCamera = Camera.open();
mCamera.startPreview();
Camera.Parameters params = mCamera.getParameters();
if(params.getFlashMode() != null){
params.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
}
mCamera.setParameters(params);
That may resolve it on some phones, other phones also require the preview to be drawn to a SurfaceView. This can be done by implementing SurfaceHolder.Callback interface in your activity.
See an example here.
It could be that the Droid X doesn't support Torch Mode. Try something like this:
List<String> pList = camera.getParameters().getSupportedFlashModes();
if (pList.contains(Parameters.FLASH_MODE_TORCH))
parameters.setFlashMode(Parameters.FLASH_MODE_TORCH);
camera.setParameters(parameters);
Refer to Issue 191453:
SurfaceTexture mDummy = new SurfaceTexture(1); // any int argument will do
camera.setPreviewTexture(mDummy);
camera.startPreview();
The only thing I found that works on the Droid X is the code presented by Siddhpura Amit part way down the page in this answer Use camera flashlight in Android. He checks the manufacturer and checks to see if it contains the string "motorola." If so, he has special code that can switch the camera Flash LED on or off. I can verify that it does work as I have a Motorola Droid X.

Categories

Resources