I am making an app that needs to use the camera flash. My code will be below this post, but I get a java.lang.RuntimeException: Fail to connect to camera service
when I try to open the camera. The error comes when the Camera.open();
method is called.
Camera camera = Camera.open();
Camera.Parameters parameters = camera.getParameters();
parameters.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
camera.setParameters(parameters);
camera.startPreview();
I have tried restarting the phone, closing all apps, and I still get the same error.
Is there a way to release/close the camera before opening it? I don't know how you would close a null object.
Screen orientation is not a problem as the app is only allowed vertical orientation.
The default camera app works, but not this app.
Error (Full):
java.lang.RuntimeException: Fail to connect to camera service
at android.hardware.Camera.native_setup(Native Method)
at android.hardware.Camera.<init>(Camera.java:319)
at android.hardware.Camera.open(Camera.java:292)
at com.bensuniverse.flashlightx.processes.CameraHandler.toggleFlash(CameraHandler.java:21)
at com.bensuniverse.flashlightx.MainActivity$1.onClick(MainActivity.java:78)
at android.view.View.performClick(View.java:4106)
at android.view.View$PerformClick.run(View.java:17052)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5059)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:792)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:555)
at dalvik.system.NativeStart.main(Native Method)