Android APIs
public final class

CameraManager

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

Class Overview

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

You can get an instance of this class by calling Context.getSystemService().

CameraManager manager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);

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. 
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.
void openCamera(String cameraId, CameraDevice.StateCallback callback, Handler handler)
Open a connection to a camera with the given ID.
void registerAvailabilityCallback(CameraManager.AvailabilityCallback callback, Handler handler)
Register a callback to be notified about camera device availability.
void unregisterAvailabilityCallback(CameraManager.AvailabilityCallback callback)
Remove a previously-added callback; the callback will no longer receive connection and disconnection callbacks.
[Expand]
Inherited Methods
From class java.lang.Object

Public Methods

public CameraCharacteristics getCameraCharacteristics (String cameraId)

Added in API level 21

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

Parameters
cameraId The id of the camera device to query
Returns
  • The properties of the given camera
Throws
IllegalArgumentException if the cameraId does not match any known camera device.
CameraAccessException if the camera is disabled by device policy, or the camera device has been disconnected.
SecurityException if the application does not have permission to access the camera

public String[] getCameraIdList ()

Added in API level 21

Return the list of currently connected camera devices by identifier.

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.

Returns
  • The list of currently connected camera devices.

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

Added in API level 21

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).

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

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

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

Parameters
cameraId The unique identifier of the camera device to open
callback The callback which is invoked once the camera is opened
handler The handler on which the callback should be invoked, or null to use the current thread's looper.
Throws
CameraAccessException if the camera is disabled by device policy, or the camera has become or was disconnected.
IllegalArgumentException if cameraId or the callback 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

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

Added in API level 21

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.

Parameters
callback the new callback to send camera availability notices to
handler The handler on which the callback should be invoked, or null to use the current thread's looper.

public void unregisterAvailabilityCallback (CameraManager.AvailabilityCallback callback)

Added in API level 21

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 The callback to remove from the notification list