I'm using the Android APIDemo sample code.
When I run the CameraPreview example, at first it was giving me an error.
I traced that one down and the sample was working for a while.
Now, it no longer works. It says
ERROR/AndroidRuntime(2949): java.lang.RuntimeException: Fail to connect to camera service
What can be causing that? It happens when camera.open() is called.
Thanks,
Tee
Be sure to properly release all the aquired camera resources:
#Override
public void surfaceDestroyed(SurfaceHolder holder) {
if (mCam != null) {
mCam.stopPreview();
mCam.setPreviewCallback(null);
mCam.release();
mCam = null;
}
}
#Override
public void surfaceCreated(SurfaceHolder holder) {
if (mCam == null) {
mCam = Camera.open();
try {
mCam.setPreviewDisplay(holder);
// TODO test how much setPreviewCallbackWithBuffer is faster
mCam.setPreviewCallback(this);
} catch (IOException e) {
mCam.release();
mCam = null;
}
}
}
Make sure your <uses-permission> elements are in the proper positions in your AndroidManifest.xml file.
It happens if your activity does not close the camera properly in surfaceDestroyed or onConfigurationChanged etc...
Don't forget to do this everytime you go out of your activity:
if (camera!=null){
camera.stopPreview();
camera.release();
camera=null;
}
Another reason of this error is when you try to open camera but some other application or even your application is already using camera.
I also get this type of issue on a HTC device. To solve add this code:
#Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
if (camera!=null)
{
camera.stopPreview();
camera.release();
camera=null;
}
}
And yet you cannot start camera then restart device.
Also, if you are using the emulator, make sure you have selected to Emulate the Front Camera and/or the Back Camera.
Android Virtual Device Manager->Select Device->Edit->Front Camera->Emulated
As others mention, you have to call release() on your camera object when you're finished.
I wasn't doing this initially, so I changed my code but it still gave me the same error. I was deploying directly to a physical handset and had to restart the phone before it worked
I also received this error when I was testing and stopped execution before reaching the point in code when the:
if (camera!=null){
camera.stopPreview();
camera.release();
camera=null;
}
was called. This then blocked the camera because it hadn't een released properly. My solution was to turn the camera off and back on again. You can confirm this is the case by trying to use the inbuilt Camera app in your phone. It won't work either because it is still busy.
Second #matt-burns however you might want to check that you're only trying to get the camera once. I had forgotten to comment out a line and was trying to launch two activities that would both try to obtain the camera.
Related
I have a live wallpaper that just draws triangles, very simple, and runs smoothly all time. After testing found that when I open the camera to take pictures the phone freezes for about 10 seconds, then the camera opens and the wallpaper crashes and the message "Unfortunately, Live Wallpaper has stopped." appears. Also found that when I open any barcode scanner app the problem raises again because the scanner uses the camera too. Seems the problem raises when the camera app runs. Any ideas what's causing it?
hi #Bullet Camera is just open ones, if it open through any other apps then, you can can not access your camera, Solution is that, where you use camera please release that after no used.
main problem is that, in your app (app in that you are using camera) , so , you release camera after used.
like following :
#Override
public void onPause() {
// TODO Auto-generated method stub
super.onPause();
if (camera != null) {
camera.stopPreview();
camera.release();
camera = null;
}
}
when you need
private void releaseCameraAndPreview() {
if (camera != null) {
camera.release();
camera = null;
}
}
or
public void stopCamera() {
if (cameraDevice != null) {
cameraDevice.stopPreview();
cameraDevice.setPreviewCallback(null);
cameraDevice.release();
cameraDevice = null;
System.out.println("in to the stop video");
}
}
My app is using camera. i'm having a CameraActivity class and CameraFragment class,
CameraFragment is responsible for releasing the camera:
#Override
public void onPause() {
super.onPause();
if (mCamera != null) {
mCamera.release();
mCamera = null;
}
}
My app crashed while taking a picture. for some reason the camera was not released. now i can't get the camera object nowhere! Also the android Camera app is not working with an error "Can't connect to the camera"
now i know that the camera is a singleton object and only one app can get it in a time.
what i don't understand is why onPause didn't run and how can i protect the camera instance better?
Also if something like that happens, how can i force release the camera object? it it a good idea?
sadly i don't have the information why the app crashed at the first place.
Thanks for your help.
Roy
This should help:
private static void unCaughtExceptionHandler() {
Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
public void uncaughtException(final Thread thread, final Throwable ex) {
ex.printStackTrace();
releaseCamera();
System.exit(0);
}
});
}
Just to be clear: this isn't about a function returning null. It's about a function throwing an Exception which turns out to be null.
throw null;
Like that.
I have a camera application which has been working fine in Ice Cream Sandwich for a while. In Gingerbread it collapses; however, I have just seen Camera.open() throw null.
try {
releaseCameraAndPreview();
mCamera = Camera.open(camera);
//and the above throws ...
} catch (Exception e) {
e.printStackTrace();
//and e is null, causing a NullPointerException
}
releaseCameraAndPreview is this:
private void releaseCameraAndPreview() {
if (mCamera != null) {
mCamera.stopPreview();
mCamera.release();
mCamera = null;
}
}
The camera is null at this point so nothing happens.
I didn't even know it was possible to throw null. What does it mean?
Restarting the phone fixed this; I can only assume the camera got into a corrupted state somehow during testing.
Re-locks the camera to prevent other processes from accessing it. Camera objects are locked by default unless unlock() is called. Normally reconnect() is used instead.
Since API level 14, camera is automatically locked for applications in start(). Applications can use the camera (ex: zoom) after recording starts. There is no need to call this after recording starts or stops.
Check if object is locked and try unlocking it if so.
https://developer.android.com/reference/android/hardware/Camera.html#lock%28%29
i have develped a camera app which uses camera API from this link
its working perfectly it shows preview takes picture but when i finish the app it never releases camera ...i can't use camera solely after installing this app ...even the app get crashed when i restart it.
here's how i am releasing the camera-->
#Override
protected void onPause() {
super.onPause();
if (camera != null) {
// preview.setCamera(null);
camera.stopPreview();
camera.setPreviewCallback(null);
camera.release();
camera = null;
}
}
onResume i put this code also but didn't work..s
#Override
protected void onResume() {
super.onResume();
camera = Camera.open();
preview.setCamera(camera);
}
I have same problems on HTC One X (4.1.2) and Nexus 4(4.3), but on Galaxy Nexus (4.3) same release method works great.
I temporary solved this issue with using camera.unlock() instead of camera.release() and keeping static reference to Camera object.
I'm doing one project with camera and after taking one photo camera freezes and u have to finish the activity and recall it again to take another photo, how can I take photo freeze for just 1-2 sec and then surface view to have the camera again. the same for video I am using media recorder, taking video press stop video saves and screen is still alive but I can not take video again I have to restart the activity?
Anybody have a solution?
I found a solution for this: After taking a picture, preview display will have stopped. To take more photos, call camera.startPreview() again first.
after capturing image you should stop the preview and start it back again.
mCamera.stopPreview();
mCamera.startPreview();
it would work fine.
Do any image processing in a background AsyncTask. This will allow your UI Activity to continue on and take another picture.
Edit: I cannot delete an accepted answer so please see stoefin's answer below. Calling camera.startPreview() before taking the next photo works for him.
The camera.startpreview(); answer didn't work for my case but the code below solved that problem for me and hope it helps others too.I used a thread to delay closing and opening of the camera after a photo is captured by 500ms
private void start_camera() {
try {
camera = Camera.open();
// camera.lock();
} catch (RuntimeException e) {
Log.e(tag, "init_camera: " + e);
return;
}
Camera.Parameters param = camera.getParameters();
param = camera.getParameters();
Camera.Size size = param.getSupportedPreviewSizes().get(0);
param.setPreviewSize(size.width, size.height);
camera.setParameters(param);
try {
camera.setPreviewDisplay(surfaceHolder);
camera.startPreview();
previewRunning = true;
} catch (Exception e) {
Log.e(tag, "init_camera: " + e);
return;
}}
private void captureImage() {
camera.takePicture(shutterCallback,null,jpegCallback);
Thread restart_preview=new Thread(){public void run(){
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
camera.release();
camera=null;
start_camera();
}};
restart_preview.start();}
Instead of using the activities defined by the existing camera app on your phone, you can write your own Activity that uses the Camera API directly to accomplish the functionality you describe. The Camera class is documented here: http://developer.android.com/reference/android/hardware/Camera.html
The camera is freezing, because you are not restarting the preview of the camera, so restart it by calling camera.startpreview()