Android Camera.autoFocus() failed

eclipseWhile coding a camera test program I run into the following problem… Camera.autoFocus(autoFocusCallback) failed the whole time with the error message „autoFocus failed“. The stacktrace contains:

java.lang.RuntimeException: autoFocus failed
at android.hardware.Camera.native_autoFocus

The internet wasn’t a real help on this topic. So I had to dig deeper on my own.
It was a bit tricky to find the error since I used a filter on my application in the logcat output. So I saw only the „autoFocus failed“ message. When I switched to „All messages“ I saw the camera is complaining that there is no active preview. Huh? But I saw the camera preview on the phone. Yeah, but this preview was created some milliseconds later than the first autoFocus request.

I use the SurfaceHolder.Callback -> surfaceCreated now to know when the surfaceHolder is created. After this I can start the AutoFocus.

Everything runs fine now 🙂

9 Antworten auf „Android Camera.autoFocus() failed“

  1. My device is Sony Xperia P (android 4.0.4). , autofocus is not working in the applications. Usually my camera runs with microspur then it will focus within 1-3 the app camera runs with microspur and doesnt focus. Any solution to this problem, PLEASE?

  2. Add this to your class defination:
    implements SurfaceHolder.Callback

    then this in onResume:

    SurfaceHolder surfaceHolder = surfaceView.getHolder();

    when surfaceCreated(SurfaceHolder holder) the surface is created and you can go on.

Schreibe einen Kommentar zu Thomas Hitziger Antworten abbrechen

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert.