CameraManager.OpenCamera Method
Definition
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Overloads
| Name | Description |
|---|---|
| OpenCamera(String, IExecutor, CameraDevice+StateCallback) |
Open a connection to a camera with the given ID. |
| OpenCamera(String, CameraDevice+StateCallback, Handler) |
Open a connection to a camera with the given ID. |
OpenCamera(String, IExecutor, CameraDevice+StateCallback)
Open a connection to a camera with the given ID.
[Android.Runtime.Register("openCamera", "(Ljava/lang/String;Ljava/util/concurrent/Executor;Landroid/hardware/camera2/CameraDevice$StateCallback;)V", "", ApiSince=28)]
[Android.Runtime.RequiresPermission("android.permission.CAMERA")]
public void OpenCamera(string cameraId, Java.Util.Concurrent.IExecutor executor, Android.Hardware.Camera2.CameraDevice.StateCallback callback);
[<Android.Runtime.Register("openCamera", "(Ljava/lang/String;Ljava/util/concurrent/Executor;Landroid/hardware/camera2/CameraDevice$StateCallback;)V", "", ApiSince=28)>]
[<Android.Runtime.RequiresPermission("android.permission.CAMERA")>]
member this.OpenCamera : string * Java.Util.Concurrent.IExecutor * Android.Hardware.Camera2.CameraDevice.StateCallback -> unit
Parameters
- cameraId
- String
The unique identifier of the camera device to open. This value cannot be null.
- executor
- IExecutor
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(). Otherwise, provide an Executor that dispatches to an appropriate thread.
- callback
- CameraDevice.StateCallback
The callback which is invoked once the camera is opened. This value cannot be null.
- Attributes
Remarks
Open a connection to a camera with the given ID.
Android reference for android.hardware.camera2.CameraManager.openCamera.
Portions of this page are modifications based on work created and shared by the Android Open Source Project and used according to terms described in the Creative Commons 2.5 Attribution License.
Applies to
OpenCamera(String, CameraDevice+StateCallback, Handler)
Open a connection to a camera with the given ID.
[Android.Runtime.Register("openCamera", "(Ljava/lang/String;Landroid/hardware/camera2/CameraDevice$StateCallback;Landroid/os/Handler;)V", "")]
[Android.Runtime.RequiresPermission("android.permission.CAMERA")]
public void OpenCamera(string cameraId, Android.Hardware.Camera2.CameraDevice.StateCallback callback, Android.OS.Handler? handler);
[<Android.Runtime.Register("openCamera", "(Ljava/lang/String;Landroid/hardware/camera2/CameraDevice$StateCallback;Landroid/os/Handler;)V", "")>]
[<Android.Runtime.RequiresPermission("android.permission.CAMERA")>]
member this.OpenCamera : string * Android.Hardware.Camera2.CameraDevice.StateCallback * Android.OS.Handler -> unit
Parameters
- cameraId
- String
The unique identifier of the camera device to open
- callback
- CameraDevice.StateCallback
The callback which is invoked once the camera is opened
- handler
- Handler
The handler on which the callback should be invoked, or
null to use the current thread's Looper.
- Attributes
Exceptions
if the camera is disabled by device policy, has been disconnected, or is being used by a higher-priority camera API client.
if cameraId or the callback was null, or the cameraId does not match any currently or previously available camera device.
if the application does not have permission to access the camera
Remarks
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, 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 dropped.
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, 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, StateCallback, Handler) call will fail and none of existing CameraDevice will be disconnected.
Since the camera device will be opened asynchronously, any asynchronous operations done on the returned CameraDevice instance will be queued up until the device startup has completed and the callback's onOpened method is called. The pending operations are then processed in order.
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 onError method will be called, and subsequent calls on the camera device will throw a CameraAccessException. Requires Manifest.permission.CAMERA
Android reference for android.hardware.camera2.CameraManager.openCamera.
Portions of this page are modifications based on work created and shared by the Android Open Source Project and used according to terms described in the Creative Commons 2.5 Attribution License.
See also
- GetCameraIdList()
- <xref:Android.App.Admin.DevicePolicyManager.SetCameraDisabled(Android.Content.ComponentName%2c+System.Boolean)>