# Print output for @column tags ?> CameraManager - Android SDK | Android Developers

Most visited

Recently visited

CameraManager

public final class CameraManager
extends Object

java.lang.Object
   ↳ android.hardware.camera2.CameraManager


A system service manager for detecting, characterizing, and connecting to CameraDevice.

For more details about communicating with camera devices, read the Camera developer guide or the camera2 package documentation.

Summary

Nested classes

class CameraManager.AvailabilityCallback

A callback for camera devices becoming available or unavailable to open. 

class CameraManager.TorchCallback

A callback for camera flash torch modes becoming unavailable, disabled, or enabled. 

Public methods

CameraCharacteristics getCameraCharacteristics(String cameraId)

Query the capabilities of a camera device.

String[] getCameraIdList()

Return the list of currently connected camera devices by identifier, including cameras that may be in use by other camera API clients.

Set<Set<String>> getConcurrentCameraIds()

Return the set of combinations of currently connected camera device identifiers, which support configuring camera device sessions concurrently.

boolean isConcurrentSessionConfigurationSupported(Map<StringSessionConfiguration> cameraIdAndSessionConfig)

Checks whether the provided set of camera devices and their corresponding SessionConfiguration can be configured concurrently.

void openCamera(String cameraId, CameraDevice.StateCallback callback, Handler handler)

Open a connection to a camera with the given ID.

void openCamera(String cameraId, Executor executor, CameraDevice.StateCallback callback)

Open a connection to a camera with the given ID.

void registerAvailabilityCallback(Executor executor, CameraManager.AvailabilityCallback callback)

Register a callback to be notified about camera device availability.

void registerAvailabilityCallback(CameraManager.AvailabilityCallback callback, Handler handler)

Register a callback to be notified about camera device availability.

void registerTorchCallback(Executor executor, CameraManager.TorchCallback callback)

Register a callback to be notified about torch mode status.

void registerTorchCallback(CameraManager.TorchCallback callback, Handler handler)

Register a callback to be notified about torch mode status.

void setTorchMode(String cameraId, boolean enabled)

Set the flash unit's torch mode of the camera of the given ID without opening the camera device.

void unregisterAvailabilityCallback(CameraManager.AvailabilityCallback callback)

Remove a previously-added callback; the callback will no longer receive connection and disconnection callbacks.

void unregisterTorchCallback(CameraManager.TorchCallback callback)

Remove a previously-added callback; the callback will no longer receive torch mode status callbacks.

Inherited methods

Public methods

getCameraCharacteristics

public CameraCharacteristics getCameraCharacteristics (String cameraId)

Query the capabilities of a camera device. These capabilities are immutable for a given camera.

From API level 29, this function can also be used to query the capabilities of physical cameras that can only be used as part of logical multi-camera. These cameras cannot be opened directly via openCamera(String, CameraDevice.StateCallback, Handler)

Also starting with API level 29, while most basic camera information is still available even without the CAMERA permission, some values are not available to apps that do not hold that permission. The keys not available are listed by CameraCharacteristics#getKeysNeedingPermission.

Parameters
cameraId String: The id of the camera device to query. This could be either a standalone camera ID which can be directly opened by openCamera(String, CameraDevice.StateCallback, Handler), or a physical camera ID that can only used as part of a logical multi-camera. This value cannot be null.

Returns
CameraCharacteristics The properties of the given camera This value cannot be null.

Throws
IllegalArgumentException if the cameraId does not match any known camera device.
CameraAccessException if the camera device has been disconnected.

See also:

getCameraIdList

public String[] getCameraIdList ()

Return the list of currently connected camera devices by identifier, including cameras that may be in use by other camera API clients.

Non-removable cameras use integers starting at 0 for their identifiers, while removable cameras have a unique identifier for each individual device, even if they are the same model.

This list doesn't contain physical cameras that can only be used as part of a logical multi-camera device.

Returns
String[] The list of currently connected camera devices. This value cannot be null.

Throws
CameraAccessException

getConcurrentCameraIds

public Set<Set<String>> getConcurrentCameraIds ()

Return the set of combinations of currently connected camera device identifiers, which support configuring camera device sessions concurrently.

The devices in these combinations can be concurrently configured by the same client camera application. Using these camera devices concurrently by two different applications is not guaranteed to be supported, however.

For concurrent operation, in chronological order : - Applications must first close any open cameras that have sessions configured, using CameraDevice#close. - All camera devices intended to be operated concurrently, must be opened using openCamera(String, CameraDevice.StateCallback, Handler), before configuring sessions on any of the camera devices.

Each device in a combination, is guaranteed to support stream combinations which may be obtained by querying getCameraCharacteristics(String) for the key CameraCharacteristics.SCALER_MANDATORY_CONCURRENT_STREAM_COMBINATIONS.

For concurrent operation, if a camera device has a non null zoom ratio range as specified by CameraCharacteristics.CONTROL_ZOOM_RATIO_RANGE, its complete zoom ratio range may not apply. Applications can use CaptureRequest.CONTROL_ZOOM_RATIO >=1 and <= CameraCharacteristics.SCALER_AVAILABLE_MAX_DIGITAL_ZOOM during concurrent operation.

The set of combinations may include camera devices that may be in use by other camera API clients.

The set of combinations doesn't contain physical cameras that can only be used as part of a logical multi-camera device.

If a new camera id becomes available through AvailabilityCallback#onCameraUnavailable(String), clients can call this method to check if new combinations of camera ids which can stream concurrently are available.

Returns
Set<Set<String>> The set of combinations of currently connected camera devices, that may have sessions configured concurrently. The set of combinations will be empty if no such combinations are supported by the camera subsystem.

Throws
CameraAccessException if the camera device has been disconnected.

isConcurrentSessionConfigurationSupported

public boolean isConcurrentSessionConfigurationSupported (Map<StringSessionConfiguration> cameraIdAndSessionConfig)

Checks whether the provided set of camera devices and their corresponding SessionConfiguration can be configured concurrently.

This method performs a runtime check of the given SessionConfiguration and camera id combinations. The result confirms whether or not the passed session configurations can be successfully used to create camera capture sessions concurrently, on the given camera devices using CameraDevice#createCaptureSession(SessionConfiguration).

The method can be called at any point before, during and after active capture sessions. It will not impact normal camera behavior in any way and must complete significantly faster than creating a regular or constrained capture session.

Although this method is faster than creating a new capture session, it is not intended to be used for exploring the entire space of supported concurrent stream combinations. The available mandatory concurrent stream combinations may be obtained by querying getCameraCharacteristics(String) for the key CameraCharacteristics.SCALER_MANDATORY_CONCURRENT_STREAM_COMBINATIONS.

Note that session parameters will be ignored and calls to SessionConfiguration#setSessionParameters are not required.


Requires Manifest.permission.CAMERA

Parameters
cameraIdAndSessionConfig Map: This value cannot be null.

Returns
boolean true if the given combination of session configurations and corresponding camera ids are concurrently supported by the camera sub-system, false otherwise OR if the set of camera devices provided is not a subset of those returned by getConcurrentCameraIds().

Throws
CameraAccessException if one of the camera devices queried is no longer connected.

openCamera

public void openCamera (String cameraId, 
                CameraDevice.StateCallback callback, 
                Handler handler)

Open a connection to a camera with the given ID.

Use getCameraIdList() to get the list of available camera devices. Note that even if an id is listed, open may fail if the device is disconnected between the calls to getCameraIdList() and openCamera(String, CameraDevice.StateCallback, Handler), or if a higher-priority camera API client begins using the camera device.

As of API level 23, devices for which the AvailabilityCallback#onCameraUnavailable(String) callback has been called due to the device being in use by a lower-priority, background camera API client can still potentially be opened by calling this method when the calling camera API client has a higher priority than the current camera API client using this device. In general, if the top, foreground activity is running within your application process, your process will be given the highest priority when accessing the camera, and this method will succeed even if the camera device is in use by another camera API client. Any lower-priority application that loses control of the camera in this way will receive an CameraDevice.StateCallback.onDisconnected(CameraDevice) callback. Opening the same camera ID twice in the same application will similarly cause the CameraDevice.StateCallback.onDisconnected(CameraDevice) callback being fired for the CameraDevice from the first open call and all ongoing tasks being droppped.

Once the camera is successfully opened, CameraDevice.StateCallback#onOpened will be invoked with the newly opened CameraDevice. The camera device can then be set up for operation by calling CameraDevice#createCaptureSession and CameraDevice#createCaptureRequest

Before API level 30, when the application tries to open multiple CameraDevice of different IDs and the device does not support opening such combination, either the openCamera(String, CameraDevice.StateCallback, Handler) will fail and throw a CameraAccessException or one or more of already opened CameraDevice will be disconnected and receive CameraDevice.StateCallback.onDisconnected(CameraDevice) callback. Which behavior will happen depends on the device implementation and can vary on different devices. Starting in API level 30, if the device does not support the combination of cameras being opened, it is guaranteed the openCamera(String, CameraDevice.StateCallback, Handler) call will fail and none of existing CameraDevice will be disconnected.

If the camera becomes disconnected during initialization after this function call returns, CameraDevice.StateCallback#onDisconnected with a CameraDevice in the disconnected state (and CameraDevice.StateCallback#onOpened will be skipped).

If opening the camera device fails, then the device callback's CameraDevice.StateCallback#onError method will be called, and subsequent calls on the camera device will throw a CameraAccessException.


Requires Manifest.permission.CAMERA

Parameters
cameraId String: The unique identifier of the camera device to open This value cannot be null.

callback CameraDevice.StateCallback: The callback which is invoked once the camera is opened This value cannot be null.

handler Handler: The handler on which the callback should be invoked, or null to use the current thread's looper. This value may be null.

Throws
CameraAccessException if the camera is disabled by device policy, has been disconnected, is being used by a higher-priority camera API client, or the device has reached its maximal resource and cannot open this camera device.
IllegalArgumentException if cameraId or the callback was null, or the cameraId does not match any currently or previously available camera device returned by getCameraIdList().
SecurityException if the application does not have permission to access the camera

See also:

openCamera

public void openCamera (String cameraId, 
                Executor executor, 
                CameraDevice.StateCallback callback)

Open a connection to a camera with the given ID.

The behavior of this method matches that of openCamera(java.lang.String, StateCallback, android.os.Handler), except that it uses Executor as an argument instead of Handler.


Requires Manifest.permission.CAMERA

Parameters
cameraId String: The unique identifier of the camera device to open This value cannot be null.

executor Executor: The executor which will be used when invoking the callback. This value cannot be null. Callback and listener events are dispatched through this Executor, providing an easy way to control which thread is used. To dispatch events through the main thread of your application, you can use Context.getMainExecutor(). To dispatch events through a shared thread pool, you can use AsyncTask#THREAD_POOL_EXECUTOR.

callback CameraDevice.StateCallback: The callback which is invoked once the camera is opened This value cannot be null.

Throws
CameraAccessException if the camera is disabled by device policy, has been disconnected, or is being used by a higher-priority camera API client.
IllegalArgumentException if cameraId, the callback or the executor was null, or the cameraId does not match any currently or previously available camera device.
SecurityException if the application does not have permission to access the camera

See also:

registerAvailabilityCallback

public void registerAvailabilityCallback (Executor executor, 
                CameraManager.AvailabilityCallback callback)

Register a callback to be notified about camera device availability.

The behavior of this method matches that of registerAvailabilityCallback(android.hardware.camera2.CameraManager.AvailabilityCallback, android.os.Handler), except that it uses Executor as an argument instead of Handler.

Parameters
executor Executor: The executor which will be used to invoke the callback. This value cannot be null. Callback and listener events are dispatched through this Executor, providing an easy way to control which thread is used. To dispatch events through the main thread of your application, you can use Context.getMainExecutor(). To dispatch events through a shared thread pool, you can use AsyncTask#THREAD_POOL_EXECUTOR.

callback CameraManager.AvailabilityCallback: the new callback to send camera availability notices to This value cannot be null.

Throws
IllegalArgumentException if the executor is null.

registerAvailabilityCallback

public void registerAvailabilityCallback (CameraManager.AvailabilityCallback callback, 
                Handler handler)

Register a callback to be notified about camera device availability.

Registering the same callback again will replace the handler with the new one provided.

The first time a callback is registered, it is immediately called with the availability status of all currently known camera devices.

AvailabilityCallback#onCameraUnavailable(String) will be called whenever a camera device is opened by any camera API client. As of API level 23, other camera API clients may still be able to open such a camera device, evicting the existing client if they have higher priority than the existing client of a camera device. See open() for more details.

Since this callback will be registered with the camera service, remember to unregister it once it is no longer needed; otherwise the callback will continue to receive events indefinitely and it may prevent other resources from being released. Specifically, the callbacks will be invoked independently of the general activity lifecycle and independently of the state of individual CameraManager instances.

Parameters
callback CameraManager.AvailabilityCallback: the new callback to send camera availability notices to This value cannot be null.

handler Handler: The handler on which the callback should be invoked, or null to use the current thread's looper. This value may be null.

Throws
IllegalArgumentException if the handler is null but the current thread has no looper.

registerTorchCallback

public void registerTorchCallback (Executor executor, 
                CameraManager.TorchCallback callback)

Register a callback to be notified about torch mode status.

The behavior of this method matches that of registerTorchCallback(android.hardware.camera2.CameraManager.TorchCallback, android.os.Handler), except that it uses Executor as an argument instead of Handler.

Parameters
executor Executor: The executor which will be used to invoke the callback This value cannot be null. Callback and listener events are dispatched through this Executor, providing an easy way to control which thread is used. To dispatch events through the main thread of your application, you can use Context.getMainExecutor(). To dispatch events through a shared thread pool, you can use AsyncTask#THREAD_POOL_EXECUTOR.

callback CameraManager.TorchCallback: The new callback to send torch mode status to This value cannot be null.

Throws
IllegalArgumentException if the executor is null.

registerTorchCallback

public void registerTorchCallback (CameraManager.TorchCallback callback, 
                Handler handler)

Register a callback to be notified about torch mode status.

Registering the same callback again will replace the handler with the new one provided.

The first time a callback is registered, it is immediately called with the torch mode status of all currently known camera devices with a flash unit.

Since this callback will be registered with the camera service, remember to unregister it once it is no longer needed; otherwise the callback will continue to receive events indefinitely and it may prevent other resources from being released. Specifically, the callbacks will be invoked independently of the general activity lifecycle and independently of the state of individual CameraManager instances.

Parameters
callback CameraManager.TorchCallback: The new callback to send torch mode status to This value cannot be null.

handler Handler: The handler on which the callback should be invoked, or null to use the current thread's looper. This value may be null.

Throws
IllegalArgumentException if the handler is null but the current thread has no looper.

setTorchMode

public void setTorchMode (String cameraId, 
                boolean enabled)

Set the flash unit's torch mode of the camera of the given ID without opening the camera device.

Use getCameraIdList() to get the list of available camera devices and use getCameraCharacteristics(String) to check whether the camera device has a flash unit. Note that even if a camera device has a flash unit, turning on the torch mode may fail if the camera device or other camera resources needed to turn on the torch mode are in use.

If setTorchMode(String, boolean) is called to turn on or off the torch mode successfully, CameraManager.TorchCallback#onTorchModeChanged will be invoked. However, even if turning on the torch mode is successful, the application does not have the exclusive ownership of the flash unit or the camera device. The torch mode will be turned off and becomes unavailable when the camera device that the flash unit belongs to becomes unavailable or when other camera resources to keep the torch on become unavailable ( CameraManager.TorchCallback#onTorchModeUnavailable will be invoked). Also, other applications are free to call setTorchMode(String, boolean) to turn off the torch mode ( CameraManager.TorchCallback#onTorchModeChanged will be invoked). If the latest application that turned on the torch mode exits, the torch mode will be turned off.

Parameters
cameraId String: The unique identifier of the camera device that the flash unit belongs to. This value cannot be null.

enabled boolean: The desired state of the torch mode for the target camera device. Set to true to turn on the torch mode. Set to false to turn off the torch mode.

Throws
CameraAccessException if it failed to access the flash unit. CameraAccessException#CAMERA_IN_USE will be thrown if the camera device is in use. CameraAccessException#MAX_CAMERAS_IN_USE will be thrown if other camera resources needed to turn on the torch mode are in use. CameraAccessException#CAMERA_DISCONNECTED will be thrown if camera service is not available.
IllegalArgumentException if cameraId was null, cameraId doesn't match any currently or previously available camera device, or the camera device doesn't have a flash unit.

unregisterAvailabilityCallback

public void unregisterAvailabilityCallback (CameraManager.AvailabilityCallback callback)

Remove a previously-added callback; the callback will no longer receive connection and disconnection callbacks.

Removing a callback that isn't registered has no effect.

Parameters
callback CameraManager.AvailabilityCallback: The callback to remove from the notification list This value cannot be null.

unregisterTorchCallback

public void unregisterTorchCallback (CameraManager.TorchCallback callback)

Remove a previously-added callback; the callback will no longer receive torch mode status callbacks.

Removing a callback that isn't registered has no effect.

Parameters
callback CameraManager.TorchCallback: The callback to remove from the notification list This value cannot be null.