Skip to content

Most visited

Recently visited

navigation

CameraDevice.StateCallback

public static abstract class CameraDevice.StateCallback
extends Object

java.lang.Object
   ↳ android.hardware.camera2.CameraDevice.StateCallback


A callback objects for receiving updates about the state of a camera device.

A callback instance must be provided to the openCamera(String, CameraDevice.StateCallback, Handler) method to open a camera device.

These state updates include notifications about the device completing startup ( allowing for createCaptureSession(List, CameraCaptureSession.StateCallback, Handler) to be called), about device disconnection or closure, and about unexpected device errors.

Events about the progress of specific CaptureRequests are provided through a CameraCaptureSession.CaptureCallback given to the capture(CaptureRequest, CameraCaptureSession.CaptureCallback, Handler), captureBurst(List, CameraCaptureSession.CaptureCallback, Handler), setRepeatingRequest(CaptureRequest, CameraCaptureSession.CaptureCallback, Handler), or setRepeatingBurst(List, CameraCaptureSession.CaptureCallback, Handler) methods.

See also:

Summary

Constants

int ERROR_CAMERA_DEVICE

An error code that can be reported by onError(CameraDevice, int) indicating that the camera device has encountered a fatal error.

int ERROR_CAMERA_DISABLED

An error code that can be reported by onError(CameraDevice, int) indicating that the camera device could not be opened due to a device policy.

int ERROR_CAMERA_IN_USE

An error code that can be reported by onError(CameraDevice, int) indicating that the camera device is in use already.

int ERROR_CAMERA_SERVICE

An error code that can be reported by onError(CameraDevice, int) indicating that the camera service has encountered a fatal error.

int ERROR_MAX_CAMERAS_IN_USE

An error code that can be reported by onError(CameraDevice, int) indicating that the camera device could not be opened because there are too many other open camera devices.

Public constructors

CameraDevice.StateCallback()

Public methods

void onClosed(CameraDevice camera)

The method called when a camera device has been closed with close().

abstract void onDisconnected(CameraDevice camera)

The method called when a camera device is no longer available for use.

abstract void onError(CameraDevice camera, int error)

The method called when a camera device has encountered a serious error.

abstract void onOpened(CameraDevice camera)

The method called when a camera device has finished opening.

Inherited methods

From class java.lang.Object

Constants

ERROR_CAMERA_DEVICE

added in API level 21
int ERROR_CAMERA_DEVICE

An error code that can be reported by onError(CameraDevice, int) indicating that the camera device has encountered a fatal error.

The camera device needs to be re-opened to be used again.

See also:

Constant Value: 4 (0x00000004)

ERROR_CAMERA_DISABLED

added in API level 21
int ERROR_CAMERA_DISABLED

An error code that can be reported by onError(CameraDevice, int) indicating that the camera device could not be opened due to a device policy.

See also:

Constant Value: 3 (0x00000003)

ERROR_CAMERA_IN_USE

added in API level 21
int ERROR_CAMERA_IN_USE

An error code that can be reported by onError(CameraDevice, int) indicating that the camera device is in use already.

This error can be produced when opening the camera fails due to the camera being used by a higher-priority camera API client.

See also:

Constant Value: 1 (0x00000001)

ERROR_CAMERA_SERVICE

added in API level 21
int ERROR_CAMERA_SERVICE

An error code that can be reported by onError(CameraDevice, int) indicating that the camera service has encountered a fatal error.

The Android device may need to be shut down and restarted to restore camera function, or there may be a persistent hardware problem.

An attempt at recovery may be possible by closing the CameraDevice and the CameraManager, and trying to acquire all resources again from scratch.

See also:

Constant Value: 5 (0x00000005)

ERROR_MAX_CAMERAS_IN_USE

added in API level 21
int ERROR_MAX_CAMERAS_IN_USE

An error code that can be reported by onError(CameraDevice, int) indicating that the camera device could not be opened because there are too many other open camera devices.

The system-wide limit for number of open cameras has been reached, and more camera devices cannot be opened until previous instances are closed.

This error can be produced when opening the camera fails.

See also:

Constant Value: 2 (0x00000002)

Public constructors

CameraDevice.StateCallback

added in API level 21
CameraDevice.StateCallback ()

Public methods

onClosed

added in API level 21
void onClosed (CameraDevice camera)

The method called when a camera device has been closed with close().

Any attempt to call methods on this CameraDevice in the future will throw a IllegalStateException.

The default implementation of this method does nothing.

Parameters
camera CameraDevice: the camera device that has become closed

This value must never be null.

onDisconnected

added in API level 21
void onDisconnected (CameraDevice camera)

The method called when a camera device is no longer available for use.

This callback may be called instead of onOpened(CameraDevice) if opening the camera fails.

Any attempt to call methods on this CameraDevice will throw a CameraAccessException. The disconnection could be due to a change in security policy or permissions; the physical disconnection of a removable camera device; or the camera being needed for a higher-priority camera API client.

There may still be capture callbacks that are invoked after this method is called, or new image buffers that are delivered to active outputs.

The default implementation logs a notice to the system log about the disconnection.

You should clean up the camera with close() after this happens, as it is not recoverable until the camera can be opened again. For most use cases, this will be when the camera again becomes available.

Parameters
camera CameraDevice: the device that has been disconnected

This value must never be null.

onError

added in API level 21
void onError (CameraDevice camera, 
                int error)

The method called when a camera device has encountered a serious error.

This callback may be called instead of onOpened(CameraDevice) if opening the camera fails.

This indicates a failure of the camera device or camera service in some way. Any attempt to call methods on this CameraDevice in the future will throw a CameraAccessException with the CAMERA_ERROR reason.

There may still be capture completion or camera stream callbacks that will be called after this error is received.

You should clean up the camera with close() after this happens. Further attempts at recovery are error-code specific.

Parameters
camera CameraDevice: The device reporting the error

This value must never be null.

error int: The error code.

Value is ERROR_CAMERA_IN_USE, ERROR_MAX_CAMERAS_IN_USE, ERROR_CAMERA_DISABLED, ERROR_CAMERA_DEVICE or ERROR_CAMERA_SERVICE.

See also:

onOpened

added in API level 21
void onOpened (CameraDevice camera)

The method called when a camera device has finished opening.

At this point, the camera device is ready to use, and createCaptureSession(List, CameraCaptureSession.StateCallback, Handler) can be called to set up the first capture session.

Parameters
camera CameraDevice: the camera device that has become opened

This value must never be null.

This site uses cookies to store your preferences for site-specific language and display options.

Get the latest Android developer news and tips that will help you find success on Google Play.

* Required Fields

Hooray!

Browse this site in ?

You requested a page in , but your language preference for this site is .

Would you like to change your language preference and browse this site in ? If you want to change your language preference later, use the language menu at the bottom of each page.

This class requires API level or higher

This doc is hidden because your selected API level for the documentation is . You can change the documentation API level with the selector above the left navigation.

For more information about specifying the API level your app requires, read Supporting Different Platform Versions.

Take a short survey?
Help us improve the Android developer experience.
(Sep 2017 survey)