3

We are getting such stacktraces in our application using cwac-camera:

java.lang.IllegalStateException: Preview mode must have started before you can take a picture
at com.commonsware.cwac.camera.CameraView.takePicture(CameraView.java:329)
at com.commonsware.cwac.camera.CameraView.takePicture(CameraView.java:277)
at com.github.randoapp.camera.RandoCameraHost.onAutoFocus(RandoCameraHost.java:119)
at com.commonsware.cwac.camera.CameraView.onAutoFocus(CameraView.java:411)
at android.hardware.Camera$EventHandler.handleMessage(Camera.java:824)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4898)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1008)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:775)
at dalvik.system.NativeStart.main(Native Method)

We believe that this can be happenning because of users pressing take picture too early. The solution is to disable button while preview is initializing and enable only when it's done. The question is: What event to use for button enabling? Is there event or callback sayong that preview is finally initialized?

Viktar Patotski
  • 584
  • 6
  • 20

2 Answers2

1

We believe that this can be happenning because of users pressing take picture too early

That is a distinct possibility.

What event to use for button enabling? Is there event or callback sayong that preview is finally initialized?

autoFocusAvailable() in your CameraHost is the best option at the moment. That is called just before the preview begins and inPreview is set to true.

I have filed an issue to do a better job than this.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • is autoFocusUnavailable() also should be used for this porposes? Because not all devices support auto focus. – Viktar Patotski Jun 27 '14 at 16:51
  • @xp-vit: That method will always be called in the current implementation. It merely means that auto-focus is possible. Again, I plan on adding better callback methods for preview availability, per the issue I cited in my answer. `autoFocusAvailable()` is your best solution in the meantime. – CommonsWare Jun 27 '14 at 16:59
  • I have the same problem but no matter how long I wait I get the error. Is there anything I can call to reset the preview? – swbandit Aug 05 '14 at 20:10
  • Oh... I just needed to call restartPreview on the fragment when the video recording stops. – swbandit Aug 05 '14 at 20:13
  • @MikeT: That's already done for you when you call `stopRecording()`. – CommonsWare Aug 05 '14 at 20:17
  • I saw that in the code. But for some reason it wasn't happening for me. The recording would stop but the video preview would stay on. – swbandit Aug 05 '14 at 20:18
  • @MikeT: Um, the preview is supposed to be on. That's automatically triggered by `stopRecording()`. – CommonsWare Aug 05 '14 at 20:29
  • @CommonsWare Can you not check for creation of surfaceview by using onSurfaceCreated callback? – Anand Prakash Oct 30 '14 at 06:47
  • @AnandPrakash: I fail to see what that has to do with the question. – CommonsWare Oct 30 '14 at 19:57
  • Never mind, actually I thought that onSurfaceCreated would give us indication that preview has started, so it is OK to take picture after that, some misconception..,anyway,I am using this solution for now- https://github.com/commonsguy/cwac-camera/issues/179 – Anand Prakash Oct 31 '14 at 02:39
  • It's still unclear to me where the appropriate place to call `autoFocusAvailable()` is supposed to be called from either this thread or the link provided. If it's supposed to be called in `CameraHost` ... is that `onCreate` or something else? – Wyatt May 12 '15 at 01:47
  • @Wyatt: You don't call `autoFocusAvailable()`. You implement it. See [the documentation](https://github.com/commonsguy/cwac-camera#auto-focus). – CommonsWare May 12 '15 at 10:01
  • @CommonsWare using a `SimpleCameraHost` and found that essentially I had to keep updating the reference to my `cameraFragment` by using a `cameraFragment = (MyCameraFragment) getFragmentManager().findFragmentByTag(TAG_CAMERA_FRAGMENT);` that than managed to solve it since this was producing a similar crash. – Wyatt May 12 '15 at 17:29
0

We got this exception whenever automatic flash was used while taking a picture in poor light conditions.

We "fixed" it by removing the inPreview check in CameraView (line 284), but I'm not sure if that wouldn't create other problems (I guess the check was there for a reason), but we haven't got any since.

davis
  • 1,137
  • 1
  • 10
  • 28