Added in [API level 21](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels) Summary: [Constants](https://developer.android.com/reference/android/hardware/camera2/CameraDevice.StateCallback#constants) \| [Ctors](https://developer.android.com/reference/android/hardware/camera2/CameraDevice.StateCallback#pubctors) \| [Methods](https://developer.android.com/reference/android/hardware/camera2/CameraDevice.StateCallback#pubmethods) \| [Inherited Methods](https://developer.android.com/reference/android/hardware/camera2/CameraDevice.StateCallback#inhmethods)

# CameraDevice.StateCallback

*** ** * ** ***

` public static abstract class CameraDevice.StateCallback `   
` extends https://developer.android.com/reference/java/lang/Object ` ` `

|---|---|
| [java.lang.Object](https://developer.android.com/reference/java/lang/Object) ||
| ↳ | android.hardware.camera2.CameraDevice.StateCallback |

<br />

*** ** * ** ***

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

A callback instance must be provided to the `https://developer.android.com/reference/android/hardware/camera2/CameraManager#openCamera(java.lang.String,%20android.hardware.camera2.CameraDevice.StateCallback,%20android.os.Handler)` method to open a camera device.

These state updates include notifications about the device completing startup ( allowing for `https://developer.android.com/reference/android/hardware/camera2/CameraDevice#createCaptureSession(android.hardware.camera2.params.SessionConfiguration)` to be called), about device disconnection or closure, and about unexpected device errors.

Events about the progress of specific `https://developer.android.com/reference/android/hardware/camera2/CaptureRequest` are provided through a `https://developer.android.com/reference/android/hardware/camera2/CameraCaptureSession.CaptureCallback` given to the `https://developer.android.com/reference/android/hardware/camera2/CameraCaptureSession#capture(android.hardware.camera2.CaptureRequest,%20android.hardware.camera2.CameraCaptureSession.CaptureCallback,%20android.os.Handler)`, `https://developer.android.com/reference/android/hardware/camera2/CameraCaptureSession#captureBurst(java.util.List<android.hardware.camera2.CaptureRequest>,%20android.hardware.camera2.CameraCaptureSession.CaptureCallback,%20android.os.Handler)`, `https://developer.android.com/reference/android/hardware/camera2/CameraCaptureSession#setRepeatingRequest(android.hardware.camera2.CaptureRequest,%20android.hardware.camera2.CameraCaptureSession.CaptureCallback,%20android.os.Handler)`, or `https://developer.android.com/reference/android/hardware/camera2/CameraCaptureSession#setRepeatingBurst(java.util.List<android.hardware.camera2.CaptureRequest>,%20android.hardware.camera2.CameraCaptureSession.CaptureCallback,%20android.os.Handler)` methods.
**See also:**

- `https://developer.android.com/reference/android/hardware/camera2/CameraManager#openCamera(java.lang.String,%20android.hardware.camera2.CameraDevice.StateCallback,%20android.os.Handler)`

## Summary

| ### Constants ||
|---|---|
| `int` | `https://developer.android.com/reference/android/hardware/camera2/CameraDevice.StateCallback#ERROR_CAMERA_DEVICE` An error code that can be reported by `https://developer.android.com/reference/android/hardware/camera2/CameraDevice.StateCallback#onError(android.hardware.camera2.CameraDevice,%20int)` indicating that the camera device has encountered a fatal error. |
| `int` | `https://developer.android.com/reference/android/hardware/camera2/CameraDevice.StateCallback#ERROR_CAMERA_DISABLED` An error code that can be reported by `https://developer.android.com/reference/android/hardware/camera2/CameraDevice.StateCallback#onError(android.hardware.camera2.CameraDevice,%20int)` indicating that the camera device could not be opened due to a device policy. |
| `int` | `https://developer.android.com/reference/android/hardware/camera2/CameraDevice.StateCallback#ERROR_CAMERA_IN_USE` An error code that can be reported by `https://developer.android.com/reference/android/hardware/camera2/CameraDevice.StateCallback#onError(android.hardware.camera2.CameraDevice,%20int)` indicating that the camera device is in use already. |
| `int` | `https://developer.android.com/reference/android/hardware/camera2/CameraDevice.StateCallback#ERROR_CAMERA_SERVICE` An error code that can be reported by `https://developer.android.com/reference/android/hardware/camera2/CameraDevice.StateCallback#onError(android.hardware.camera2.CameraDevice,%20int)` indicating that the camera service has encountered a fatal error. |
| `int` | `https://developer.android.com/reference/android/hardware/camera2/CameraDevice.StateCallback#ERROR_MAX_CAMERAS_IN_USE` An error code that can be reported by `https://developer.android.com/reference/android/hardware/camera2/CameraDevice.StateCallback#onError(android.hardware.camera2.CameraDevice,%20int)` indicating that the camera device could not be opened because there are too many other open camera devices. |

| ### Public constructors ||
|---|---|
| ` https://developer.android.com/reference/android/hardware/camera2/CameraDevice.StateCallback#StateCallback()() ` |

| ### Public methods ||
|---|---|
| ` void` | ` https://developer.android.com/reference/android/hardware/camera2/CameraDevice.StateCallback#onClosed(android.hardware.camera2.CameraDevice)(https://developer.android.com/reference/android/hardware/camera2/CameraDevice camera) ` The method called when a camera device has been closed with `https://developer.android.com/reference/android/hardware/camera2/CameraDevice#close()`. |
| ` abstract void` | ` https://developer.android.com/reference/android/hardware/camera2/CameraDevice.StateCallback#onDisconnected(android.hardware.camera2.CameraDevice)(https://developer.android.com/reference/android/hardware/camera2/CameraDevice camera) ` The method called when a camera device is no longer available for use. |
| ` abstract void` | ` https://developer.android.com/reference/android/hardware/camera2/CameraDevice.StateCallback#onError(android.hardware.camera2.CameraDevice,%20int)(https://developer.android.com/reference/android/hardware/camera2/CameraDevice camera, int error) ` The method called when a camera device has encountered a serious error. |
| ` abstract void` | ` https://developer.android.com/reference/android/hardware/camera2/CameraDevice.StateCallback#onOpened(android.hardware.camera2.CameraDevice)(https://developer.android.com/reference/android/hardware/camera2/CameraDevice camera) ` The method called when a camera device has finished opening. |

### Inherited methods

From class ` https://developer.android.com/reference/java/lang/Object ` ` https://developer.android.com/reference/java/lang/Object` ` https://developer.android.com/reference/java/lang/Object#clone()() `

Creates and returns a copy of this object.
` boolean` ` https://developer.android.com/reference/java/lang/Object#equals(java.lang.Object)(https://developer.android.com/reference/java/lang/Object obj) `

Indicates whether some other object is "equal to" this one.
` void` ` https://developer.android.com/reference/java/lang/Object#finalize()() `

Called by the garbage collector on an object when garbage collection determines that there are no more references to the object.
` final https://developer.android.com/reference/java/lang/Class<?>` ` https://developer.android.com/reference/java/lang/Object#getClass()() `

Returns the runtime class of this `Object`.
` int` ` https://developer.android.com/reference/java/lang/Object#hashCode()() `

Returns a hash code value for the object.
` final void` ` https://developer.android.com/reference/java/lang/Object#notify()() `

Wakes up a single thread that is waiting on this object's monitor.
` final void` ` https://developer.android.com/reference/java/lang/Object#notifyAll()() `

Wakes up all threads that are waiting on this object's monitor.
` https://developer.android.com/reference/java/lang/String` ` https://developer.android.com/reference/java/lang/Object#toString()() `

Returns a string representation of the object.
` final void` ` https://developer.android.com/reference/java/lang/Object#wait(long,%20int)(long timeoutMillis, int nanos) `

Causes the current thread to wait until it is awakened, typically by being *notified* or *interrupted*, or until a certain amount of real time has elapsed.
` final void` ` https://developer.android.com/reference/java/lang/Object#wait(long)(long timeoutMillis) `

Causes the current thread to wait until it is awakened, typically by being *notified* or *interrupted*, or until a certain amount of real time has elapsed.
` final void` ` https://developer.android.com/reference/java/lang/Object#wait()() `

Causes the current thread to wait until it is awakened, typically by being *notified* or *interrupted*.

## Constants

### ERROR_CAMERA_DEVICE

Added in [API level 21](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public static final int ERROR_CAMERA_DEVICE
```

An error code that can be reported by `https://developer.android.com/reference/android/hardware/camera2/CameraDevice.StateCallback#onError(android.hardware.camera2.CameraDevice,%20int)` indicating that the camera device has encountered a fatal error.

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

<br />

<br />

**See also:**

- `https://developer.android.com/reference/android/hardware/camera2/CameraDevice.StateCallback#onError(android.hardware.camera2.CameraDevice,%20int)`

Constant Value: 4 (0x00000004)

### ERROR_CAMERA_DISABLED

Added in [API level 21](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public static final int ERROR_CAMERA_DISABLED
```

An error code that can be reported by `https://developer.android.com/reference/android/hardware/camera2/CameraDevice.StateCallback#onError(android.hardware.camera2.CameraDevice,%20int)` indicating that the camera device could not be opened due to a device policy.

<br />

**See also:**

- `https://developer.android.com/reference/android/app/admin/DevicePolicyManager#setCameraDisabled(android.content.ComponentName,%20boolean)`
- `https://developer.android.com/reference/android/hardware/camera2/CameraDevice.StateCallback#onError(android.hardware.camera2.CameraDevice,%20int)`

Constant Value: 3 (0x00000003)

### ERROR_CAMERA_IN_USE

Added in [API level 21](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public static final int ERROR_CAMERA_IN_USE
```

An error code that can be reported by `https://developer.android.com/reference/android/hardware/camera2/CameraDevice.StateCallback#onError(android.hardware.camera2.CameraDevice,%20int)` 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.

<br />

<br />

**See also:**

- `https://developer.android.com/reference/android/hardware/camera2/CameraDevice.StateCallback#onError(android.hardware.camera2.CameraDevice,%20int)`

Constant Value: 1 (0x00000001)

### ERROR_CAMERA_SERVICE

Added in [API level 21](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public static final int ERROR_CAMERA_SERVICE
```

An error code that can be reported by `https://developer.android.com/reference/android/hardware/camera2/CameraDevice.StateCallback#onError(android.hardware.camera2.CameraDevice,%20int)` 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.

<br />

<br />

**See also:**

- `https://developer.android.com/reference/android/hardware/camera2/CameraDevice.StateCallback#onError(android.hardware.camera2.CameraDevice,%20int)`

Constant Value: 5 (0x00000005)

### ERROR_MAX_CAMERAS_IN_USE

Added in [API level 21](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public static final int ERROR_MAX_CAMERAS_IN_USE
```

An error code that can be reported by `https://developer.android.com/reference/android/hardware/camera2/CameraDevice.StateCallback#onError(android.hardware.camera2.CameraDevice,%20int)` 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.

<br />

<br />

**See also:**

- `https://developer.android.com/reference/android/hardware/camera2/CameraDevice.StateCallback#onError(android.hardware.camera2.CameraDevice,%20int)`

Constant Value: 2 (0x00000002)

## Public constructors

### StateCallback

```
public StateCallback ()
```

<br />

<br />

## Public methods

### onClosed

Added in [API level 21](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public void onClosed (CameraDevice camera)
```

The method called when a camera device has been closed with `https://developer.android.com/reference/android/hardware/camera2/CameraDevice#close()`.

Any attempt to call methods on this CameraDevice in the future will throw a `https://developer.android.com/reference/java/lang/IllegalStateException`.

The default implementation of this method does nothing.

<br />

<br />

| Parameters ||
|---|---|
| `camera` | `CameraDevice`: the camera device that has become closed. This value cannot be `null`. <br /> |

### onDisconnected

Added in [API level 21](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public abstract void onDisconnected (CameraDevice camera)
```

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

This callback may be called instead of `https://developer.android.com/reference/android/hardware/camera2/CameraDevice.StateCallback#onOpened(android.hardware.camera2.CameraDevice)` if opening the camera fails.

Any attempt to call methods on this CameraDevice will throw a `https://developer.android.com/reference/android/hardware/camera2/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 `https://developer.android.com/reference/android/hardware/camera2/CameraDevice#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 `https://developer.android.com/reference/android/hardware/camera2/CameraManager.AvailabilityCallback#onCameraAvailable(java.lang.String)`.

<br />

<br />

| Parameters ||
|---|---|
| `camera` | `CameraDevice`: the device that has been disconnected. This value cannot be `null`. <br /> |

### onError

Added in [API level 21](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public abstract 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 `https://developer.android.com/reference/android/hardware/camera2/CameraDevice.StateCallback#onOpened(android.hardware.camera2.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 `https://developer.android.com/reference/android/hardware/camera2/CameraAccessException` with the `https://developer.android.com/reference/android/hardware/camera2/CameraAccessException#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 `https://developer.android.com/reference/android/hardware/camera2/CameraDevice#close()` after this happens. Further attempts at recovery are error-code specific.

<br />

<br />

| Parameters ||
|---|---|
| `camera` | `CameraDevice`: The device reporting the error. This value cannot be `null`. <br /> |
| `error` | `int`: The error code. Value is one of the following: - `https://developer.android.com/reference/android/hardware/camera2/CameraDevice.StateCallback#ERROR_CAMERA_IN_USE` - `https://developer.android.com/reference/android/hardware/camera2/CameraDevice.StateCallback#ERROR_MAX_CAMERAS_IN_USE` - `https://developer.android.com/reference/android/hardware/camera2/CameraDevice.StateCallback#ERROR_CAMERA_DISABLED` - `https://developer.android.com/reference/android/hardware/camera2/CameraDevice.StateCallback#ERROR_CAMERA_DEVICE` - `https://developer.android.com/reference/android/hardware/camera2/CameraDevice.StateCallback#ERROR_CAMERA_SERVICE` |

**See also:**

- `https://developer.android.com/reference/android/hardware/camera2/CameraDevice.StateCallback#ERROR_CAMERA_IN_USE`
- `https://developer.android.com/reference/android/hardware/camera2/CameraDevice.StateCallback#ERROR_MAX_CAMERAS_IN_USE`
- `https://developer.android.com/reference/android/hardware/camera2/CameraDevice.StateCallback#ERROR_CAMERA_DISABLED`
- `https://developer.android.com/reference/android/hardware/camera2/CameraDevice.StateCallback#ERROR_CAMERA_DEVICE`
- `https://developer.android.com/reference/android/hardware/camera2/CameraDevice.StateCallback#ERROR_CAMERA_SERVICE`

### onOpened

Added in [API level 21](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public abstract 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 `https://developer.android.com/reference/android/hardware/camera2/CameraDevice#createCaptureSession(android.hardware.camera2.params.SessionConfiguration)` can be called to set up the first capture session.

<br />

<br />

| Parameters ||
|---|---|
| `camera` | `CameraDevice`: the camera device that has become opened. This value cannot be `null`. <br /> |