Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android camera: Threads? Which should do what

I am trying to figure out which threads should do what in Android.

The only thing I have found stated in the official documentation is that camera.open() should be put into its own thread.

What about:

  • camera.startPreview()
  • camera.stopPreview()
  • camera.release()

It doesn't state which thread they need. Must they be run on the main thread (ui thread)? Or am I free to choose?

Why am I trying to figure this out? camera.startPreview() when run on the main thread is causing my app to jitter/lag for a short period of time, this heavily affects my application as it is put inside a viewPager and I do not wish to have the camera to always preview (which would cause no lag, but takes up system resources).

Any ideas?

like image 466
basickarl Avatar asked May 12 '15 20:05

basickarl


People also ask

Which action should be performed on the main thread Android?

The main thread is responsible for dispatching events to the appropriate user interface widgets as well as communicating with components from the Android UI toolkit. To keep your application responsive, it is essential to avoid using the main thread to perform any operation that may end up keeping it blocked.

What is main thread and background thread in Android?

For example, if your app makes a network request from the main thread, your app's UI is frozen until it receives the network response. You can create additional background threads to handle long-running operations while the main thread continues to handle UI updates.

How does the threading work in Android?

When an application component starts and the application does not have any other components running, the Android system starts a new Linux process for the application with a single thread of execution. By default, all components of the same application run in the same process and thread (called the "main" thread).


1 Answers

The documentation for Camera states that the class is not thread safe and should not be called from multiple threads at once (I suppose, unless you are performing your own synchronization).

It says that the callbacks will be delivered to the thread that makes the call to open

From the reference (emphasis mine):

This class is not thread-safe, and is meant for use from one event thread. Most long-running operations (preview, focus, photo capture, etc) happen asynchronously and invoke callbacks as necessary. Callbacks will be invoked on the event thread open(int) was called from. This class's methods must never be called from multiple threads at once.

From the open(int) method reference:

Callbacks from other methods are delivered to the event loop of the thread which called open(). If this thread has no event loop, then callbacks are delivered to the main application event loop. If there is no main application event loop, callbacks are not delivered.

Caution: On some devices, this method may take a long time to complete. It is best to call this method from a worker thread (possibly using AsyncTask) to avoid blocking the main application UI thread.

The thread it needs is the one you use to call open(int).

So to answer your question, yes you are relatively free to choose, but you must remain consistent.

like image 131
Ryan J Avatar answered Sep 29 '22 09:09

Ryan J