# CameraState

Artifact: [androidx.camera:camera-core](https://developer.android.com/jetpack/androidx/releases/camera) [View Source](https://cs.android.com/search?q=file:androidx/camera/core/CameraState.java+class:androidx.camera.core.CameraState) Added in [1.1.0](https://developer.android.com/jetpack/androidx/releases/camera#1.1.0)

*** ** * ** ***


```
@AutoValue
abstract class CameraState
```

<br />

*** ** * ** ***

Represents the different states the camera can be in.

The following table displays the states the camera can be in, and the possible transitions between them.

| State | Transition cause | New State |
|---|---|---|
| CLOSED | Received signal to open camera, and camera unavailable | PENDING_OPEN |
| Received signal to open camera, and camera available | OPENING |
| PENDING_OPEN | Received signal that camera is available | OPENING |
| OPENING | Camera opened successfully | OPEN |
| Camera encountered recoverable error while opening | OPENING(Error) |
| Camera encountered critical error while opening | CLOSING(Error) |
| Camera opening failed prematurely | CLOSED(Error) |
| Reached max limit of camera (re)open attempts | PENDING_OPEN |
| OPEN | Camera encountered recoverable error | OPENING(Error) |
| Camera encountered critical error | CLOSING(Error) |
| Received signal to close camera | CLOSING |
| CLOSING | Camera closed | CLOSED |

Initially, a camera is in a `https://developer.android.com/reference/kotlin/androidx/camera/core/CameraState.Type#CLOSED` state. When it receives a signal to open, for example after one or multiple `https://developer.android.com/reference/kotlin/androidx/camera/core/UseCase` are attached to it, its state moves to the `https://developer.android.com/reference/kotlin/androidx/camera/core/CameraState.Type#OPENING` state. If it successfully opens the camera device, its state moves to the `https://developer.android.com/reference/kotlin/androidx/camera/core/CameraState.Type#OPEN` state, otherwise, it may move to a different state depending on the error it encountered:

- If the camera is physically removed (e.g. a USB camera is unplugged), its state will move directly to `https://developer.android.com/reference/kotlin/androidx/camera/core/CameraState.Type#CLOSED` with an `https://developer.android.com/reference/kotlin/androidx/camera/core/CameraState#ERROR_CAMERA_REMOVED()` error.
- If opening the camera device fails prematurely, for example, when "Do Not Disturb" mode is enabled on a device that's affected by a bug in Android 9 (see `https://developer.android.com/reference/kotlin/androidx/camera/core/CameraState#ERROR_DO_NOT_DISTURB_MODE_ENABLED()`), the state moves to the `https://developer.android.com/reference/kotlin/androidx/camera/core/CameraState.Type#CLOSED` state .
- If the error is recoverable, CameraX will attempt to reopen the camera device. If a recovery attempt succeeds, the camera state moves to the `https://developer.android.com/reference/kotlin/androidx/camera/core/CameraState.Type#OPEN` state, however, if all recovery attempts are unsuccessful, the camera waits in a `https://developer.android.com/reference/kotlin/androidx/camera/core/CameraState.Type#PENDING_OPEN` state to attempt recovery again once the camera device's availability changes.
- If the error is critical, and requires the intervention of the developer or user, the camera's state moves to the `https://developer.android.com/reference/kotlin/androidx/camera/core/CameraState.Type#CLOSING` state.

While in the `https://developer.android.com/reference/kotlin/androidx/camera/core/CameraState.Type#PENDING_OPEN` state, the camera waits for a signal indicating the camera device's availability. The signal can either be an external one from the camera service, or an internal one from within CameraX. When received, the camera's state moves to the `https://developer.android.com/reference/kotlin/androidx/camera/core/CameraState.Type#OPENING` state, and an attempt to open the camera device is made.

While in the `https://developer.android.com/reference/kotlin/androidx/camera/core/CameraState.Type#OPEN` state, the camera device may be disconnected due to an error. In this case, depending on whether the error is critical or recoverable, CameraX may or may not attempt to recover from it, thus the state will move to either a `https://developer.android.com/reference/kotlin/androidx/camera/core/CameraState.Type#CLOSING` or `https://developer.android.com/reference/kotlin/androidx/camera/core/CameraState.Type#OPENING` state.

If the camera is in an `https://developer.android.com/reference/kotlin/androidx/camera/core/CameraState.Type#OPEN` state and receives a signal to close the camera device, for example when all its previously attached `https://developer.android.com/reference/kotlin/androidx/camera/core/UseCase` are detached, its state moves to the `https://developer.android.com/reference/kotlin/androidx/camera/core/CameraState.Type#CLOSING` state. Once the camera device finishes closing, the camera state moves to the `https://developer.android.com/reference/kotlin/androidx/camera/core/CameraState.Type#CLOSED` state.

Whenever the camera encounters an error, it reports it through `https://developer.android.com/reference/kotlin/androidx/camera/core/CameraState#getError()`.

## Summary

| ### Nested types |
|---|
| `enum https://developer.android.com/reference/kotlin/androidx/camera/core/CameraState.ErrorType` Types of errors the camera can encounter. |
| `@https://www.javadoc.io/doc/com.google.auto.value/auto-value/latest/com/google/auto/value/AutoValue.html abstract class https://developer.android.com/reference/kotlin/androidx/camera/core/CameraState.StateError` Error that the camera has encountered. |
| `enum https://developer.android.com/reference/kotlin/androidx/camera/core/CameraState.Type` States the camera can be in. |

| ### Constants |
|---|---|
| `const https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | `https://developer.android.com/reference/kotlin/androidx/camera/core/CameraState#ERROR_CAMERA_DISABLED() = 5` An error indicating that the camera device could not be opened due to a device policy. |
| `const https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | `https://developer.android.com/reference/kotlin/androidx/camera/core/CameraState#ERROR_CAMERA_FATAL_ERROR() = 6` An error indicating that the camera device was closed due to a fatal error. |
| `const https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | `https://developer.android.com/reference/kotlin/androidx/camera/core/CameraState#ERROR_CAMERA_IN_USE() = 2` An error indicating that the camera device is already in use. |
| `const https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | `https://developer.android.com/reference/kotlin/androidx/camera/core/CameraState#ERROR_CAMERA_REMOVED() = 8` An error indicating that the camera device is no longer available because it has been removed from the system. |
| `const https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | `https://developer.android.com/reference/kotlin/androidx/camera/core/CameraState#ERROR_DO_NOT_DISTURB_MODE_ENABLED() = 7` An error indicating that the camera could not be opened because "Do Not Disturb" mode is enabled on devices affected by a bug in Android 9 (API level 28). |
| `const https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | `https://developer.android.com/reference/kotlin/androidx/camera/core/CameraState#ERROR_MAX_CAMERAS_IN_USE() = 1` An error indicating that the limit number of open cameras has been reached, and more cameras cannot be opened until other instances are closed. |
| `const https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | `https://developer.android.com/reference/kotlin/androidx/camera/core/CameraState#ERROR_OTHER_RECOVERABLE_ERROR() = 3` An error indicating that the camera device has encountered a recoverable error. |
| `const https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | `https://developer.android.com/reference/kotlin/androidx/camera/core/CameraState#ERROR_STREAM_CONFIG() = 4` An error indicating that configuring the camera has failed. |

| ### Public constructors |
|---|
| `https://developer.android.com/reference/kotlin/androidx/camera/core/CameraState#CameraState()()` |

| ### Public functions |
|---|---|
| `java-static https://developer.android.com/reference/kotlin/androidx/camera/core/CameraState` | `https://developer.android.com/reference/kotlin/androidx/camera/core/CameraState#create(androidx.camera.core.CameraState.Type)(type: https://developer.android.com/reference/kotlin/androidx/camera/core/CameraState.Type)` Create a new `https://developer.android.com/reference/kotlin/androidx/camera/core/CameraState` instance from a `https://developer.android.com/reference/kotlin/androidx/camera/core/CameraState.Type` and a `null``https://developer.android.com/reference/kotlin/androidx/camera/core/CameraState.StateError`. |
| `java-static https://developer.android.com/reference/kotlin/androidx/camera/core/CameraState` | `https://developer.android.com/reference/kotlin/androidx/camera/core/CameraState#create(androidx.camera.core.CameraState.Type,androidx.camera.core.CameraState.StateError)(type: https://developer.android.com/reference/kotlin/androidx/camera/core/CameraState.Type, error: https://developer.android.com/reference/kotlin/androidx/camera/core/CameraState.StateError?)` Create a new `https://developer.android.com/reference/kotlin/androidx/camera/core/CameraState` instance from a `https://developer.android.com/reference/kotlin/androidx/camera/core/CameraState.Type` and a potential `https://developer.android.com/reference/kotlin/androidx/camera/core/CameraState.StateError`. |
| `abstract https://developer.android.com/reference/kotlin/androidx/camera/core/CameraState.StateError?` | `https://developer.android.com/reference/kotlin/androidx/camera/core/CameraState#getError()()` Potentially returns an error the camera encountered. |
| `abstract https://developer.android.com/reference/kotlin/androidx/camera/core/CameraState.Type` | `https://developer.android.com/reference/kotlin/androidx/camera/core/CameraState#getType()()` Returns the camera's state. |

## Constants

### ERROR_CAMERA_DISABLED

Added in [1.1.0](https://developer.android.com/jetpack/androidx/releases/camera#1.1.0)

```
const val ERROR_CAMERA_DISABLED = 5: Int
```

An error indicating that the camera device could not be opened due to a device policy.

The error may be encountered if a client from a background process attempts to open the camera.

| See also |
|---|---|
| `https://developer.android.com/reference/android/app/admin/DevicePolicyManager.html#setCameraDisabled(android.content.ComponentName, boolean)` |   |

### ERROR_CAMERA_FATAL_ERROR

Added in [1.1.0](https://developer.android.com/jetpack/androidx/releases/camera#1.1.0)

```
const val ERROR_CAMERA_FATAL_ERROR = 6: Int
```

An error indicating that the camera device was closed due to a fatal error.

The error may require the Android device to be shut down and restarted to restore camera function. It may also indicate the existence of a persistent camera hardware problem. When CameraX uses a `https://developer.android.com/reference/android/hardware/camera2/package-summary.html` implementation, this error represents a `https://developer.android.com/reference/android/hardware/camera2/CameraDevice.StateCallback.html#ERROR_CAMERA_SERVICE` error.

### ERROR_CAMERA_IN_USE

Added in [1.1.0](https://developer.android.com/jetpack/androidx/releases/camera#1.1.0)

```
const val ERROR_CAMERA_IN_USE = 2: Int
```

An error indicating that the camera device is already in use.

This could be due to the camera device being used by a higher-priority camera client.

### ERROR_CAMERA_REMOVED

Added in [1.6.0](https://developer.android.com/jetpack/androidx/releases/camera#1.6.0)

```
const val ERROR_CAMERA_REMOVED = 8: Int
```

An error indicating that the camera device is no longer available because it has been removed from the system.

This error will be reported when a camera is disconnected from the host device (e.g., a USB camera is unplugged). This is a terminal state for the camera session. Once this error is received, the associated `https://developer.android.com/reference/kotlin/androidx/camera/core/Camera` and `https://developer.android.com/reference/kotlin/androidx/camera/core/CameraInfo` objects are no longer valid. Attempting to call methods on them may result in exceptions.

**Action:** The application should unbind all use cases from the invalid camera and switch to another available camera. To find a new camera, use `https://developer.android.com/reference/kotlin/androidx/camera/lifecycle/ProcessCameraProvider#getAvailableCameraInfos()` or `https://developer.android.com/reference/kotlin/androidx/camera/lifecycle/ProcessCameraProvider#hasCamera(androidx.camera.core.CameraSelector)`.

This error is considered critical, and CameraX will not attempt to recover.

### ERROR_DO_NOT_DISTURB_MODE_ENABLED

Added in [1.1.0](https://developer.android.com/jetpack/androidx/releases/camera#1.1.0)

```
const val ERROR_DO_NOT_DISTURB_MODE_ENABLED = 7: Int
```

An error indicating that the camera could not be opened because "Do Not Disturb" mode is enabled on devices affected by a bug in Android 9 (API level 28).

When "Do Not Disturb" mode is enabled, opening the camera device fails on certain Android devices running on an early Android 9 release with a `https://developer.android.com/reference/android/hardware/camera2/CameraMetadata.html#INFO_SUPPORTED_HARDWARE_LEVEL_LEGACY` camera hardware level.

CameraX will not attempt to reopen the camera device, instead, disable the "Do Not Disturb" mode, then explicitly open the camera again.

### ERROR_MAX_CAMERAS_IN_USE

Added in [1.1.0](https://developer.android.com/jetpack/androidx/releases/camera#1.1.0)

```
const val ERROR_MAX_CAMERAS_IN_USE = 1: Int
```

An error indicating that the limit number of open cameras has been reached, and more cameras cannot be opened until other instances are closed.

### ERROR_OTHER_RECOVERABLE_ERROR

Added in [1.1.0](https://developer.android.com/jetpack/androidx/releases/camera#1.1.0)

```
const val ERROR_OTHER_RECOVERABLE_ERROR = 3: Int
```

An error indicating that the camera device has encountered a recoverable error.

CameraX will attempt to recover from the error, it if succeeds in doing so, the camera will open, otherwise the camera will move to a `https://developer.android.com/reference/kotlin/androidx/camera/core/CameraState.Type#PENDING_OPEN` state. When CameraX uses a `https://developer.android.com/reference/android/hardware/camera2/package-summary.html` implementation, this error represents a `https://developer.android.com/reference/android/hardware/camera2/CameraDevice.StateCallback.html#ERROR_CAMERA_DEVICE` error.

### ERROR_STREAM_CONFIG

Added in [1.1.0](https://developer.android.com/jetpack/androidx/releases/camera#1.1.0)

```
const val ERROR_STREAM_CONFIG = 4: Int
```

An error indicating that configuring the camera has failed.

## Public constructors

### CameraState

Added in 1.1.0

```
CameraState()
```

## Public functions

### create

Added in [1.1.0](https://developer.android.com/jetpack/androidx/releases/camera#1.1.0)

```
java-static fun create(type: CameraState.Type): CameraState
```

Create a new `https://developer.android.com/reference/kotlin/androidx/camera/core/CameraState` instance from a `https://developer.android.com/reference/kotlin/androidx/camera/core/CameraState.Type` and a `null``https://developer.android.com/reference/kotlin/androidx/camera/core/CameraState.StateError`.

A `https://developer.android.com/reference/kotlin/androidx/camera/core/CameraState` is not expected to be instantiated in normal operation.

### create

Added in [1.1.0](https://developer.android.com/jetpack/androidx/releases/camera#1.1.0)

```
java-static fun create(type: CameraState.Type, error: CameraState.StateError?): CameraState
```

Create a new `https://developer.android.com/reference/kotlin/androidx/camera/core/CameraState` instance from a `https://developer.android.com/reference/kotlin/androidx/camera/core/CameraState.Type` and a potential `https://developer.android.com/reference/kotlin/androidx/camera/core/CameraState.StateError`.

A `https://developer.android.com/reference/kotlin/androidx/camera/core/CameraState` is not expected to be instantiated in normal operation.

### getError

Added in [1.1.0](https://developer.android.com/jetpack/androidx/releases/camera#1.1.0)

```
abstract fun getError(): CameraState.StateError?
```

Potentially returns an error the camera encountered.

| Returns |
|---|---|
| `https://developer.android.com/reference/kotlin/androidx/camera/core/CameraState.StateError?` | An error the camera encountered, or `null` otherwise. |

### getType

Added in [1.1.0](https://developer.android.com/jetpack/androidx/releases/camera#1.1.0)

```
abstract fun getType(): CameraState.Type
```

Returns the camera's state.

| Returns |
|---|---|
| `https://developer.android.com/reference/kotlin/androidx/camera/core/CameraState.Type` | The camera's state |