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.AvailabilityListener A listener for camera devices becoming available or unavailable to open. 
Public Methods
void addAvailabilityListener(CameraManager.AvailabilityListener listener, Handler handler)
Register a listener to be notified about camera device availability.
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.StateListener listener, Handler handler)
Open a connection to a camera with the given ID.
void removeAvailabilityListener(CameraManager.AvailabilityListener listener)
Remove a previously-added listener; the listener will no longer receive connection and disconnection callbacks.
[Expand]
Inherited Methods
From class java.lang.Object

Public Methods

public void addAvailabilityListener (CameraManager.AvailabilityListener listener, Handler handler)

Register a listener to be notified about camera device availability.

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

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

public CameraCharacteristics getCameraCharacteristics (String cameraId)

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 currently connected camera device.
CameraAccessException if the camera is disabled by device policy.
SecurityException if the application does not have permission to access the camera

public String[] getCameraIdList ()

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.StateListener listener, 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.StateListener, 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.StateListener, 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 listener'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
listener The listener which is invoked once the camera is opened
handler The handler on which the listener 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 listener 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 removeAvailabilityListener (CameraManager.AvailabilityListener listener)

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

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

Parameters
listener The listener to remove from the notification list