Following is the code which I used for opening Torch and closing it. But when I close it, it crashes. LogCat says " Runtime Exception : Fail to connect to camera service "!
+
hasFlash is not getting any value and is throwing Nullpointer exception. (I'm using it to check if the flash is present or not.)
What am I doing Wrong?
boolean hasFlash = this.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);
if(hasFlash==true)
{
if(s.equalsIgnoreCase("FlashLight On") || s.equalsIgnoreCase("Flash Light On"))
{
Camera cam = Camera.open();
Parameters p = cam.getParameters();
p.setFlashMode(Parameters.FLASH_MODE_TORCH);
cam.setParameters(p);
return "Turning on";
}
if(s.equalsIgnoreCase("FlashLight Off") || s.equalsIgnoreCase("Flash Light Off"))
{
Camera cam = Camera.open();
Parameters p = cam.getParameters();
p.setFlashMode(Parameters.FLASH_MODE_OFF);
cam.setParameters(p);
cam.stopPreview();
cam.release();
return "Turning off";
}
}
else
{
return "Flash Not Available";
}
Change
Camera cam = Camera.open();
Parameters p = cam.getParameters();
p.setFlashMode(Parameters.FLASH_MODE_OFF);
cam.setParameters(p);
cam.stopPreview();
cam.release();
return "Turning off";
to
cam.stopPreview();
cam.release();
return "Turning off";
I ran into a lot of these issues building an open source flashlight for Android which may help you with further questions.
Flashlight by Joe github
Related
I am getting following error
Exception java.lang.RuntimeException: setParameters failed
android.hardware.Camera.native_setParameters (Camera.java)
android.hardware.Camera.setParameters (Camera.java:1946)
in code below. I have no clue what wrong i am doing below.
Camera mCamera = Camera.open();
Parameters params = mCamera.getParameters();
if (params.getFlashMode() != null)
params.setFlashMode(Parameters.FLASH_MODE_OFF);
if (nightMode && params.getSceneMode() != null)
params.setSceneMode(Parameters.SCENE_MODE_NIGHT);
if (params.getSupportedFocusModes().contains(Parameters.FOCUS_MODE_CONTINUOUS_VIDEO)) {
params.setFocusMode(Parameters.FOCUS_MODE_CONTINUOUS_VIDEO);
} else if (params.getSupportedFocusModes().contains(Parameters.FOCUS_MODE_CONTINUOUS_PICTURE)) {
params.setFocusMode(Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);
} else if (params.getSupportedFocusModes().contains(Camera.Parameters.FOCUS_MODE_INFINITY)) {
params.setFocusMode(Parameters.FOCUS_MODE_INFINITY);
}
mCamera.setParameters(params);
this error is occurring in some devices like samsung mostly.
Requesting for help.Thanks in advance.
Your params could be not supported by device. You can detect available focus modes with getSupportedFocusModes method of Camera.Parameters class. If some mode doesn't contain in this list, you can't set it to your camera.
Edit
As Alex said in comment you can see error message in logcat.
you must check if the focus mode supported by the user device, not all devices has camera focus mode, you do it like this:
public boolean support_focus(Camera camera){
Camera.Parameters parameters = camera.getParameters();
List<String> focusModes = parameters.getSupportedFocusModes();
if (focusModes.contains(Camera.Parameters.FOCUS_MODE_AUTO))
return true;
else
return false;
}
this check if device support FOCUS_MODE_AUTO, change that with your desired Parameter.
I am using following code to turn LED flashlight on and off:
public Flashlight(SurfaceView preview, Context context){
this.preview = preview;
this.context = context;
mHolder = preview.getHolder(); //mHolder is surfaceHolder
mHolder.addCallback(this);
try {
mCamera = Camera.open();
params = mCamera.getParameters();
mCamera.setPreviewDisplay(mHolder);
mCamera.startPreview();
//AUTOFOCUS LASER FIX ON LG G3
List<String> focusModes = params.getSupportedFocusModes();
if (focusModes.contains(params.FOCUS_MODE_INFINITY)) {
params.setFocusMode(params.FOCUS_MODE_INFINITY);
}
else{
if (focusModes.contains(params.FOCUS_MODE_FIXED))
params.setFocusMode(params.FOCUS_MODE_FIXED);
}
mCamera.setParameters(params);
cameraOpened = true;
}catch (Exception e){
cameraOpened = false;
e.printStackTrace();
}
}
private void turnOnFlashlight(){
flashlightOn = true;
params.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
mCamera.setParameters(params);
}
private void turnOffFlashlight(){
flashlightOn = false;
params.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
mCamera.setParameters(params);
}
It works great on most phones but I cant get it working on xperia Z5. I dont have Z5 for testing so I only know it from user response. So I would like to ask if there is any other (preferably working) way to turn on flashlight on Xperia Z5.
Thanks in forward
As already mentioned in the comment, I found there to be 3 steps of making the flash appear (seems to be working on all devices so far)
cam.setParameters(p); // will trigger flash on most devices
// Needed for some devices.
cam.setPreviewTexture(new SurfaceTexture(0));
// Needed for some more devices.
cam.startPreview();
Since you did 2 of those, try adding the PreviewTexture and it should work. The whole code of a working flashlight can be found here at Flashlight Widget
What I would do is simply turn on flash led of my phone by press a button. As I could read, it appear too much simple, but code I've found doesn't work!
This is how I turn on led at click on button: +
private void cameraOn() {
params = camera.getParameters();
params.setFlashMode(Parameters.FLASH_MODE_TORCH);
camera.setParameters(params);
camera.startPreview();
torch_button.setText("Switch off");
isTorchOn = true;
}
Params and camera object was initialized inside onCreate method. No error is thrown, but light doesn't switch on.
what's wrong?
Looks like this one has possibly already been answered
How to turn on camera flash light programmatically in Android?
But basically you need to have the correct permissions.
<!-- Allows access to the flashlight -->
<permission android:name="android.permission.FLASHLIGHT"
android:permissionGroup="android.permission-group.HARDWARE_CONTROLS"
android:protectionLevel="normal"
android:label="#string/permlab_flashlight"
android:description="#string/permdesc_flashlight" />
The problem could be another application holding the camera. Your code seems to be right.
I had done it successfully with this code :
Parameters p = null;
try {
p = camera.getParameters();
} catch (Exception e) {
e.printStackTrace();
}
if (isLightOn) {
Log.e("info", "turning off!");
p.setFlashMode(Parameters.FLASH_MODE_OFF);
camera.setParameters(p);
camera.stopPreview();
isLightOn = false;
} else {
Log.e("info", "turning on!");
p.setFlashMode(Parameters.FLASH_MODE_TORCH);
camera.setParameters(p);
camera.startPreview();
isLightOn = true;
}
I want to blink flashlight on incoming call. Everytime this code is going in service method i.e its running fine, its starting the service fine but the problem is its unable to start camera service everytime. If camera is running the blinking is not stopping if the call state changes. How could i start camera everytime and stop the blinking if the call state changes??
The code i used...
in manifest
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.FLASHLIGHT"/>
in service
String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
if(state.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
try {
cam = Camera.open();
p = cam.getParameters();
String myString = "0101010101010101010101010101010101010101010101011";
long blinkDelay = 150;
cam.setParameters(p);
for (int i = 0; i < myString.length(); i++) {
if (state.equals(TelephonyManager.EXTRA_STATE_IDLE)){
break;
}else if (state.equals(TelephonyManager.EXTRA_STATE_OFFHOOK)){
break;
}
if (myString.charAt(i) == '0') {
p.setFlashMode(Parameters.FLASH_MODE_TORCH);
cam.setParameters(p);
} else {
p.setFlashMode(Parameters.FLASH_MODE_OFF);
cam.setParameters(p);
}
try {
Thread.sleep(blinkDelay);
} catch (Exception e) {
Log.d(tag, e.toString());
p.setFlashMode(Parameters.FLASH_MODE_OFF);
cam.setParameters(p);
}
}
}catch (Exception e) {
// TODO: handle exception
Log.d(tag, "in catch1");
Log.d(tag, e.toString());
}
}else if (state.equals(TelephonyManager.EXTRA_STATE_IDLE)){
cam.release();
stopSelf();
}else if (state.equals(TelephonyManager.EXTRA_STATE_OFFHOOK)){
cam.release();
stopSelf();
}
}
The logcat is not stopping and it is still running , crazy is the tag that i declared i got this
03-11 23:11:08.129: W/CameraService(133): CameraService::connect X (pid 942) rejected (existing client).
03-11 23:11:08.139: D/crazy(942): in catch1
03-11 23:11:08.139: D/crazy(942): java.lang.RuntimeException: Fail to connect to camera service
If you check out the Camera documentation, one reason that opening the camera could throw a RuntimeException is that another process is using the camera, which is what the error log seems to indicate. Check what other processes are running and make sure nothing else is using the camera (you should also be properly handling this error).
You should also make sure your application is calling release() on the camera, so that you aren't blocking other applications from using it.
How I check the flash light available on device?also want to know how can I On/Off the flash light?
I have put the code but not working right now?
I search out this
http://gitorious.org/rowboat/frameworks-base/commit/eb9cbb8fdddf4c887004b20b504083035d57a15f
http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/2.2_r1.1/com/android/server/LightsService.java#LightsService
Please can tell which I should use?
Thank You.
You can use the following
context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);
which will return true if a flash is available, false if not.
See http://developer.android.com/reference/android/content/pm/PackageManager.html for more information.
boolean hasFlash =this.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);
or
public boolean hasFlash() {
if (camera == null) {
return false;
}
Camera.Parameters parameters = camera.getParameters();
if (parameters.getFlashMode() == null) {
return false;
}
List<String> supportedFlashModes = parameters.getSupportedFlashModes();
if (supportedFlashModes == null || supportedFlashModes.isEmpty() || supportedFlashModes.size() == 1 && supportedFlashModes.get(0).equals(Camera.Parameters.FLASH_MODE_OFF)) {
return false;
}
return true;
}
First you get supported flash modes:
camera = Camera.open(i); // Introduced in API level 9
parameters = camera.getParameters();
String[] flashModes = parameters.getSupportedFlashModes();
And then you check if this array contains the correct constants like: "auto", "on", "off".
More info in:
http://developer.android.com/reference/android/hardware/Camera.Parameters.html#FLASH_MODE_AUTO
This can help full to turn on/off flash light of device. This is give me satisfaction, Hope be useful to you also.
Turn On camera Flash
camera = Camera.open();
Parameters p = camera.getParameters();
p.setFlashMode(Parameters.FLASH_MODE_TORCH);
camera.setParameters(p);
camera.startPreview();
Turn Off camera Flash
camera = Camera.open();
Parameters p = camera.getParameters();
p.setFlashMode(Parameters.FLASH_MODE_OFF);
camera.setParameters(p);
camera.stopPreview();
Put this Permission in manifest file
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
For more detail you can go HERE.