CameraPresenceListener


public interface CameraPresenceListener


A listener for changes in camera presence, indicating when cameras are added to or removed from the set of devices that are recognized by the system and usable by CameraX.

This listener reports on changes to the set of cameras that CameraX determines can be bound and used. This "usable" set is derived by filtering the list of all available cameras based on several criteria:

  • The cameras reported as available by the device's framework.
  • The camera meeting the minimum requirements for use with the CameraX library.
  • The application of any CameraSelector limiters configured to CameraX during initialization.

Important: This listener focuses on the presence of usable cameras, not their temporary availability due to other apps using them. It does not report temporary changes in availability, such as when a camera becomes temporarily in-use by another application. For observing those states on an open camera, see CameraState.

Register this listener via addCameraPresenceListener or addCameraPresenceListener.

Summary

Public methods

abstract void

Called when one or more new cameras have been added to the list of usable cameras.

abstract void

Called when one or more previously usable cameras have been removed from the system.

Public methods

onCamerasAdded

Added in 1.6.0-alpha01
abstract void onCamerasAdded(@NonNull Set<CameraIdentifier> cameraIdentifiers)

Called when one or more new cameras have been added to the list of usable cameras.

On initial registration of the listener, **if there are any cameras currently present and usable**, this method will be called once with a set containing all of them. Subsequent calls will only contain cameras that have been newly added since the last update.

Upon receiving this callback, an application can iterate through the provided set and may choose to switch to one of the new cameras. A specific CameraIdentifier can then be used with of to create a precise selector for binding.

Parameters
@NonNull Set<CameraIdentifier> cameraIdentifiers

The non-empty set of identifiers for the cameras that were added.

onCamerasRemoved

Added in 1.6.0-alpha01
abstract void onCamerasRemoved(@NonNull Set<CameraIdentifier> cameraIdentifiers)

Called when one or more previously usable cameras have been removed from the system.

Important: When this callback is invoked, the cameras it refers to are no longer operational. Their corresponding Camera, CameraInfo, and CameraControl objects should be considered invalid. The provided identifiers should be used as keys to identify which cameras were removed.

Action: If any of the removed cameras were in use, the application should unbind all UseCases that were bound to the removed camera. This is necessary to release resources and allow the UseCases to be bound to another camera. Afterward, the application can attempt to switch to another available camera or update its UI to reflect that the camera is no longer present.

Event Order Guarantee: If any of the removed cameras were currently open, their CameraState will have already transitioned to CLOSED with an error of ERROR_CAMERA_REMOVEDbefore this callback is invoked.

Parameters
@NonNull Set<CameraIdentifier> cameraIdentifiers

The non-empty set of identifiers for the cameras that were removed.