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

# StateCallback

*** ** * ** ***

Kotlin \|[Java](https://developer.android.com/reference/android/hardware/camera2/CameraDevice.StateCallback "View this page in Java")

```
abstract class StateCallback
```

|---|---|
| [kotlin.Any](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-any/index.html) ||
| ↳ | [android.hardware.camera2.CameraDevice.StateCallback](https://developer.android.com/reference/kotlin/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 android.hardware.camera2.CameraManager#openCamera method to open a camera device.

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

Events about the progress of specific `https://developer.android.com/reference/kotlin/android/hardware/camera2/CaptureRequest` are provided through a `https://developer.android.com/reference/kotlin/android/hardware/camera2/CameraCaptureSession.CaptureCallback` given to the `https://developer.android.com/reference/kotlin/android/hardware/camera2/CameraCaptureSession#capture(android.hardware.camera2.CaptureRequest,%20android.hardware.camera2.CameraCaptureSession.CaptureCallback,%20android.os.Handler)`, `https://developer.android.com/reference/kotlin/android/hardware/camera2/CameraCaptureSession#captureBurst(kotlin.collections.MutableList,%20android.hardware.camera2.CameraCaptureSession.CaptureCallback,%20android.os.Handler)`, `https://developer.android.com/reference/kotlin/android/hardware/camera2/CameraCaptureSession#setRepeatingRequest(android.hardware.camera2.CaptureRequest,%20android.hardware.camera2.CameraCaptureSession.CaptureCallback,%20android.os.Handler)`, or `https://developer.android.com/reference/kotlin/android/hardware/camera2/CameraCaptureSession#setRepeatingBurst(kotlin.collections.MutableList,%20android.hardware.camera2.CameraCaptureSession.CaptureCallback,%20android.os.Handler)` methods.

## Summary

| Constants ||
|---|---|
| static [Int](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-int/index.html) | `https://developer.android.com/reference/kotlin/android/hardware/camera2/CameraDevice.StateCallback#ERROR_CAMERA_DEVICE:kotlin.Int` An error code that can be reported by `https://developer.android.com/reference/kotlin/android/hardware/camera2/CameraDevice.StateCallback#onError(android.hardware.camera2.CameraDevice,%20kotlin.Int)` indicating that the camera device has encountered a fatal error. |
| static [Int](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-int/index.html) | `https://developer.android.com/reference/kotlin/android/hardware/camera2/CameraDevice.StateCallback#ERROR_CAMERA_DISABLED:kotlin.Int` An error code that can be reported by `https://developer.android.com/reference/kotlin/android/hardware/camera2/CameraDevice.StateCallback#onError(android.hardware.camera2.CameraDevice,%20kotlin.Int)` indicating that the camera device could not be opened due to a device policy. |
| static [Int](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-int/index.html) | `https://developer.android.com/reference/kotlin/android/hardware/camera2/CameraDevice.StateCallback#ERROR_CAMERA_IN_USE:kotlin.Int` An error code that can be reported by `https://developer.android.com/reference/kotlin/android/hardware/camera2/CameraDevice.StateCallback#onError(android.hardware.camera2.CameraDevice,%20kotlin.Int)` indicating that the camera device is in use already. |
| static [Int](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-int/index.html) | `https://developer.android.com/reference/kotlin/android/hardware/camera2/CameraDevice.StateCallback#ERROR_CAMERA_SERVICE:kotlin.Int` An error code that can be reported by `https://developer.android.com/reference/kotlin/android/hardware/camera2/CameraDevice.StateCallback#onError(android.hardware.camera2.CameraDevice,%20kotlin.Int)` indicating that the camera service has encountered a fatal error. |
| static [Int](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-int/index.html) | `https://developer.android.com/reference/kotlin/android/hardware/camera2/CameraDevice.StateCallback#ERROR_MAX_CAMERAS_IN_USE:kotlin.Int` An error code that can be reported by `https://developer.android.com/reference/kotlin/android/hardware/camera2/CameraDevice.StateCallback#onError(android.hardware.camera2.CameraDevice,%20kotlin.Int)` 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/kotlin/android/hardware/camera2/CameraDevice.StateCallback#StateCallback()()` <br /> |

| Public methods ||
|---|---|
| open [Unit](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-unit/index.html) | `https://developer.android.com/reference/kotlin/android/hardware/camera2/CameraDevice.StateCallback#onClosed(android.hardware.camera2.CameraDevice)(camera: https://developer.android.com/reference/kotlin/android/hardware/camera2/CameraDevice)` The method called when a camera device has been closed with `https://developer.android.com/reference/kotlin/android/hardware/camera2/CameraDevice#close()`. |
| abstract [Unit](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-unit/index.html) | `https://developer.android.com/reference/kotlin/android/hardware/camera2/CameraDevice.StateCallback#onDisconnected(android.hardware.camera2.CameraDevice)(camera: https://developer.android.com/reference/kotlin/android/hardware/camera2/CameraDevice)` The method called when a camera device is no longer available for use. |
| abstract [Unit](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-unit/index.html) | `https://developer.android.com/reference/kotlin/android/hardware/camera2/CameraDevice.StateCallback#onError(android.hardware.camera2.CameraDevice,%20kotlin.Int)(camera: https://developer.android.com/reference/kotlin/android/hardware/camera2/CameraDevice, error: https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-int/index.html)` The method called when a camera device has encountered a serious error. |
| abstract [Unit](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-unit/index.html) | `https://developer.android.com/reference/kotlin/android/hardware/camera2/CameraDevice.StateCallback#onOpened(android.hardware.camera2.CameraDevice)(camera: https://developer.android.com/reference/kotlin/android/hardware/camera2/CameraDevice)` The method called when a camera device has finished opening. |

## Constants

### ERROR_CAMERA_DEVICE

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

```
static val ERROR_CAMERA_DEVICE: Int
```

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

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

    Value: 4

**See Also**

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

### ERROR_CAMERA_DISABLED

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

```
static val ERROR_CAMERA_DISABLED: Int
```

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

    Value: 3

**See Also**

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

### ERROR_CAMERA_IN_USE

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

```
static val ERROR_CAMERA_IN_USE: Int
```

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

    Value: 1

**See Also**

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

### ERROR_CAMERA_SERVICE

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

```
static val ERROR_CAMERA_SERVICE: Int
```

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

    Value: 5

**See Also**

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

### ERROR_MAX_CAMERAS_IN_USE

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

```
static val ERROR_MAX_CAMERAS_IN_USE: Int
```

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

    Value: 2

**See Also**

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

## Public constructors

### StateCallback

```
StateCallback()
```

## Public methods

### onClosed

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

```
open fun onClosed(camera: CameraDevice): Unit
```

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

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

The default implementation of this method does nothing.

| Parameters ||
|---|---|
| `camera` | [CameraDevice](https://developer.android.com/reference/kotlin/android/hardware/camera2/CameraDevice): the camera device that has become closed. This value cannot be `null`. |

### onDisconnected

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

```
abstract fun onDisconnected(camera: CameraDevice): Unit
```

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/kotlin/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/kotlin/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/kotlin/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/kotlin/android/hardware/camera2/CameraManager.AvailabilityCallback#onCameraAvailable(kotlin.String)`.

| Parameters ||
|---|---|
| `camera` | [CameraDevice](https://developer.android.com/reference/kotlin/android/hardware/camera2/CameraDevice): the device that has been disconnected. This value cannot be `null`. |

### onError

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

```
abstract fun onError(
    camera: CameraDevice, 
    error: Int
): Unit
```

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

This callback may be called instead of `https://developer.android.com/reference/kotlin/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/kotlin/android/hardware/camera2/CameraAccessException` with the `https://developer.android.com/reference/kotlin/android/hardware/camera2/CameraAccessException#CAMERA_ERROR:kotlin.Int` 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/kotlin/android/hardware/camera2/CameraDevice#close()` after this happens. Further attempts at recovery are error-code specific.

| Parameters ||
|---|---|
| `camera` | [CameraDevice](https://developer.android.com/reference/kotlin/android/hardware/camera2/CameraDevice): The device reporting the error. This value cannot be `null`. |
| `error` | [Int](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-int/index.html): The error code. Value is one of the following: - `https://developer.android.com/reference/kotlin/android/hardware/camera2/CameraDevice.StateCallback#ERROR_CAMERA_IN_USE:kotlin.Int` - `https://developer.android.com/reference/kotlin/android/hardware/camera2/CameraDevice.StateCallback#ERROR_MAX_CAMERAS_IN_USE:kotlin.Int` - `https://developer.android.com/reference/kotlin/android/hardware/camera2/CameraDevice.StateCallback#ERROR_CAMERA_DISABLED:kotlin.Int` - `https://developer.android.com/reference/kotlin/android/hardware/camera2/CameraDevice.StateCallback#ERROR_CAMERA_DEVICE:kotlin.Int` - `https://developer.android.com/reference/kotlin/android/hardware/camera2/CameraDevice.StateCallback#ERROR_CAMERA_SERVICE:kotlin.Int` |

**See Also**

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

### onOpened

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

```
abstract fun onOpened(camera: CameraDevice): Unit
```

The method called when a camera device has finished opening.

At this point, the camera device is ready to use, and android.hardware.camera2.CameraDevice#createCaptureSession can be called to set up the first capture session.

| Parameters ||
|---|---|
| `camera` | [CameraDevice](https://developer.android.com/reference/kotlin/android/hardware/camera2/CameraDevice): the camera device that has become opened. This value cannot be `null`. |