45

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.

Grimmace
  • 4,021
  • 1
  • 31
  • 25
  • I'm wondering if you find any solution – onur taskin Apr 17 '13 at 07:07
  • 1
    +onur taskin - See my answer below. It explains how I fixed it, as well as how others may get into trouble with it. – Grimmace Apr 20 '13 at 17:52
  • In my case, I was getting this on the stock Camera app on an Android API 22 emulator. I fixed it by using the 32bit x86 OS image on the emulator instead of the 64bit x86 one. – Joshua Pinter Oct 30 '17 at 02:34
  • For emulators see https://stackoverflow.com/questions/44586150/android-camera-getparameters-failed-empty-parameters. – CoolMind Feb 10 '19 at 21:51

4 Answers4

37

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().

joel.wilson
  • 8,243
  • 5
  • 28
  • 48
Xor
  • 371
  • 3
  • 3
29

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.

Grimmace
  • 4,021
  • 1
  • 31
  • 25
  • 1
    Bear in mind that even after resolving your problem, the exception may still be thrown as the camera service can end up in a bad state. If the device's Camera app also doesn't show a picture, only a reboot will resolve the problem. – Paul Lammertsma Jul 24 '13 at 20:24
3

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.

Eddy Talvala
  • 17,243
  • 2
  • 42
  • 47
  • Im seeing this issue today even though I've implemented CameraTwo - And its only Razor devices causing the issue. – JCutting8 Jun 01 '21 at 02:15
1

Camera objects are always locked by default, so when you call unlock method then you allow to other processes to use your parameters. So make sure that you re locked the camera before getting parameters

Oriol Roma
  • 329
  • 1
  • 5
  • 9