As pointed out in the Android documentation on handling the camera, it is recommended to use a separate thread to open the camera.
Well, I am doing just that but do have some difficulties:
For my camera object I am using a global instance variable. Now, when I start my app, I create a separate thread in onResume()
and do all initializations for that camera object in that thread.
Later when I leave the app, I release the camera in onPause()
. This works all just fine.
But the problem is: When I did some stress tests and switched really fast between onResume()
and onPause()
(by hitting the multitask button excessively fast) my app crashed. The reason was that there was a Method called after release()
.
This makes sense since it is possible that the camera may be released in onPause()
but at the same time the thread hasn't finished with its initialization. Thus, the thread tries to make a call on a camera object that was already released.
Now, what can I do to fix this? Maybe not use a global camera object? Or how can I make this thread safe?