Correct handling of exception: "getParameters failed (empty parameters)"
In my case I was getting this error :
getParameters failed (empty parameters)
when I called getParameters()
after unlocking the camera. So, please call getParameters()
before you call camera.unlock()
.
Is there a specific Android device that experiences this error? Or do you see it across many devices.
In general, you should not see this kind of an error. It's possible your application has some sort of race condition which results in this, but it'd have to involve trying to call getParameters on an uninitialized or already-released camera.
It could also be an error in the device-specific camera code, or a rare race condition somewhere in the camera code stack. Without more detail (logcat or Android bugreport from such a crash), it's impossible to tell - the error itself just says that the device-specific camera code returned an empty set of parameters.
But once you get this error, there's not a lot you can do - the camera subsystem is in some odd state. If you want to try to deal with it, all I can suggest is to close and reopen the camera device.
camera objects are always locked by default so when you can unlock method then you allow to other procceses to use your parameters so make sure that you re locked the camera before getting parameters
As +Eddy Talvala mentioned, this happens when the camera is in a bad state.
How does the camera get in a bad state?
1) Probably the most common reason would be closing/releasing the camera while still using it afterward. This can be especially problematic if you are using the Camera
object on multiple threads without synchronizing access to the Camera
. Make sure you only ever have a single thread accessing the Camera
at a time.
2) In my case it was a bit more tricky. I use a SurfaceTexture
so that I can use the camera output as an OpenGL texture. In Android 4.0 (ICS), there is a new method SurfaceTexture.release()
. This method is important to use when using SurfaceTexture
s as it cleans up the memory quicker than it previously did.
The problem is that I was calling SurfaceTexture.release()
while the camera preview was still active. This was crashing the Camera
service, which was causing the issue explained in the question.
In my case I fixed it by delaying the call to SurfaceTexture.release()
until after I had replaced it with a new SurfaceTexture
. This way I was certain the SurfaceTexture
could be cleaned up without any bad side-effects.