Search code examples
androidandroid-camera

Correct handling of exception: "getParameters failed (empty parameters)"


I have a camera app in the Google Play store with Google Analytics installed. I keep getting the following crash report:

getParameters failed (empty parameters)

My question is: What is the correct way to handle this?

Looking into the Android source of where it happens didn't give me any extra details. The error is thrown in android_hardware_Camera.cpp:

String8 params8 = camera->getParameters();
if (params8.isEmpty()) {
    jniThrowRuntimeException(env, "getParameters failed (empty parameters)");
    return 0;
}

Looking into open source Android camera to see how it handles the situation was also not very helpful. That code doesn't appear to catch the RuntimeException when calling getParameters. (Except in one case where they catch it, close the camera, then rethrow it).

Is there a correct way to handle this?

If not, is there a reason this happens so often?

Note: On any given day I have between 5k - 8k active users. With somewhere between 40-70 of these exceptions. That seems really high to me. I know there are legit instances where a camera may fail to initialize. But 1% of users seems unreasonable. Also, since the Android camera app doesn't handle the exception it really makes me wonder if there is some other root cause.


Solution

  • 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 SurfaceTextures 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.