While 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 🙂
Thanks .. This helped to solve my problem …
Thank you! It was really tricky.
what’s the meaning of SurfaceHolder.Callback -> surfaceCreated
It’s described in the Android Developer pages.
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 seconds.in the app camera runs with microspur and doesnt focus. Any solution to this problem, PLEASE?
Could you explain the „SurfaceHolder.Callback -> surfaceCreated“ ?Thanks!
Add this to your class defination:
implements SurfaceHolder.Callback
then this in onResume:
SurfaceHolder surfaceHolder = surfaceView.getHolder();
surfaceHolder.addCallback(this);
when surfaceCreated(SurfaceHolder holder) the surface is created and you can go on.
Thanks. Good spot. This was my problem exactly. Calling autoFocus too early!