After stop camera service, the camera does not work anymore - android

Is there any way to correctly stop the camera service? This does not seem to work:
public void surfaceDestroyed(SurfaceHolder holder){
camera.stopPreview();
camera = null;
}
After doing that the camera does not work, even if I use the program that is preinstalled in my handset.

public void surfaceDestroyed(SurfaceHolder holder) {
camera.stopPreview();
camera.release();
camera=null;
}
This works fine for me. Make sure your surfaceDestroyed() method is getting invoked.

If you are using the the preview callback you might want to deregister it in your surfaceDestroyed
mCamera.setPreviewCallback(null);
mCamera is supposed to be your Camera object

Related

GLSurfaceView switching issue

I am having an issue with while switching GLSurfaceView. I have a GLSurfaceView (connected with Camera) and I want to move it to another view , but when I am moving it it's losing its frames and showing blackScreen after switching. and when I am reconnecting it to Camera, it starts show my Camera but with delay.
Could any one please tell me solution to move it to another parent without losing its frames (because when I am reconnecting it its showing frames with 2-3 seconds delay).
Please find code written below :
(Here VideoPanel (extends GLSurfaceView) is Frame provided by oovooSDK to show video)
final VideoPanel movingVideoView = parent1.getChildAt(0);
movingView.onPause();
parent1.removeView(movingView);
parent2.addView(movingView);
movingView.onResume();
movingView.requestRender();
for reconnecting :
application.unbindVideoPanel(videoView.userId, videoView.videoRender);
application.unbindVideoPanel(videoView.userId, videoView.videoRender);
// this methods shows delay of 2-3 seconds for binding view again
I tried following methods also
((VideoPanel) videoView.videoRender).setPreserveEGLContextOnPause(true);
// to save context when paused
((VideoPanel)videoView.videoRender).setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY); // to render frame again when requestRender is called
Camera is not proper release when your activity going to end.
If you are using a Surface view than release your camera in onSurfaceDestroy
public void surfaceDestroyed(SurfaceHolder holder) {
if(camera!=null){
camera.stopPreview();
camera.setPreviewCallback(null);
camera.release();
camera = null;
}
}
Also recommend a release your camera if it's not ever going to use.
protected void onDestroy(){
if(camera!=null){
camera.stopPreview();
camera.setPreviewCallback(null);
camera.release();
camera = null;
}
}

camera.release() not working

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.

recreating camera object after error 100 (camera server died)

I have classic android app with camera preview (common implem that can be found in many tutorials [marakana etc.]) that is supposed to take picture in a given time interval. Threading and killing threads is done, errors such "method called after release" are handled. But sometimes the well-known error 100 occurs. I accepted the fact that it happens and tried to handle it too. I implemented ErrorCallback and its onError method where the current camera object is released and instantiated a new one as written in official documentation.
But (with no surprise) it is not enough. New camera is maybe wrongly allocated because an message "CameraDemo has been exited unexpectedly" appears now.
I've read many docs and examples in hope, that a proper proceeding will be somewhere explained but no one has such problem apparently. So I would like to ask what else should I do beside releasing and creating new camera? Here is the code:
ErrorCallback CEC = new ErrorCallback()
{
public void onError(int error, Camera camera)
{
Log.d("CameraDemo", "camera error detected");
if(error == Camera.CAMERA_ERROR_SERVER_DIED)
{
Log.d("CameraDemo", "attempting to reinstantiate new camera");
camera.stopPreview();
camera.setPreviewCallback(null);
camera.release(); //written in documentation...
camera = null;
camera = Camera.open();
}
}
};
Shortly - if I release and recreate camera in onError callback then RuntimeException Method called after release (takePicture) is raised. So should I somehow assign the surface holder to camera again or recreate the surface holder too?
It would be enough to direct me e.g. to some forums, where it is described or solved, etc. Thanks for any help.
In my app to handle the camere i use this :
public void onResume() {
super.onResume();
if(mCamera == null)
mCamera = getCameraInstance():
}
public static Camera getCameraInstance() {
mCamera = null;
try {
mCamera = Camera.open();
Parameters parameters = mCamera.getParameters();
mCamera.cancelAutoFocus();
mCamera.setPreviewCallback(yourPreviewCb);
mCamera.startPreview();
mCamera.setParameters(parameters);
mCamera.autoFocus(yourAutoFocusCB);
} catch (Exception e) {
//TODO
}
return mCamera;
}
The mCamera = null in the getCameraInstance() is just to be sure there is no camera running at all.
I think you need to recreate a complete camera, not just open it with the
camera.open();
Set this in the onResume or in the error callback, depending on your needs.
This is how I fixed it, here is a sample of code, think you get the idea:
private Camera camera;
// code...
public Camera getCameraInstance() {
Camera camera = Camera.open();
// code...
camera.setErrorCallback(new ErrorCallback() {
#Override
public void onError(int error, Camera camera) {
if(error == Camera.CAMERA_ERROR_SERVER_DIED) {
releaseCamera();
startCamera();
}
}
});
return camera;
}
protected void startCamera() {
if(getCamera() == null)
setCamera(getCameraInstance());
refreshCamera();
}
protected void releaseCamera() {
if (getCamera() != null) {
getCamera().release();
setCamera(null);
}
}
public Camera getCamera() {
return camera;
}
public void setCamera(Camera camera) {
this.camera = camera;
}

Camera onPreviewFrame not called

when using the Camera.PreviewCallback implementation the onPreviewFrame is called without problem after initializing camera and starting preview (Camera.startPrevew()). The problem is if I make a video recording using MediaRecorder onPreviewFrame does not get called any more.
I understand that when using MediaRecorder to record video stops the Camera.PreviewCallback, but why can't it be restarted?
I have tried resetting the camera preview callback (setPreviewCallback(callback)) and restarting startPreview, but while I have a preview there is no call to onPreviewFrame.
You must call setPreviewCallback in the surfaceChanged method, not only in the surfaceCreated.
public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
if (mHolder.getSurface() == null){
return;
}
try {
mCamera.stopPreview();
} catch (Exception e){
// ignore: tried to stop a non-existent preview
}
try {
mCamera.setPreviewCallback(this);
mCamera.setPreviewDisplay(mHolder);
mCamera.startPreview();
} catch (Exception e){
Log.d(TAG, "Error starting camera preview: " + e.getMessage());
}
}
I had a similar problem; see
setOneShotPreviewCallback not hitting onPreviewFrame() in callback
What I discovered was that after calling Camera#unlock() to prepare the MediaRecorder, it was necessary to call Camera#reconnect() before setting the preview callback. This is because Camera.unlock() detaches the camera from the process to let the MediaRecorder connect to it.
http://developer.android.com/reference/android/hardware/Camera.html#unlock()
In my investigations I also discovered that if you set any preview callbacks using other methods than the one shot method, you have to reset all of these after calling Camera#reconnect() as well. So, briefly:
mCamera.unlock();
//set up MediaRecorder
mCamera.reconnect();
mCamera.setPreviewCallback(mCallback);
//or whatever callback method you want to use
//and even if you've set this callback already
I hope that helps!
You should call it within new instantiation of previewCallBacks() interface, like below
public void surfaceCreated(SurfaceHolder holder) {
// if (mediaRecorder == null) {
try {
camera = Camera.open();
camera.setPreviewCallback(new PreviewCallback() {
public void onPreviewFrame(byte[] _data, Camera _camera) {
}
}
}
}
You need to call startPreview() again after a video or photo was taken.

Android - Fail to connect to camera

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.

Categories

Resources