CameraManager.AvailabilityCallback provides an onCameraAvailable(String cameraId) method to determine if a camera is available or not? https://developer.android.com/reference/android/hardware/camera2/CameraManager.AvailabilityCallback.html
Retrieving all camera IDs is the same as above, as shown in @GAGAN.
CameraManager camManager = (CameraManager) mContext.getSystemService(Context.CAMERA_SERVICE); // Usually front mCamera is at 0 position. try { cameraId = camManager.getCameraIdList()[0]; } catch (CameraAccessException e) { Log.e(TAG, Log.getStackTraceString(e)); Log.cat("Error: ", e.getReason()+""); if(e.CAMERA_DISABLED) {//ENABLE CAMERA } else if( ... you can go to the below link to do various logic) .... else { //don nothing} }
https://developer.android.com/reference/android/hardware/camera2/CameraAccessException.html#CAMERA_IN_USE
Note. While other applications do not use the hardware of your camera, they use them purposefully. Therefore, until these applications release your hardware, you cannot use it, that’s clear. You may not know if these applications are really needed for the camera. We consider hardware backup systems when necessary.
But we can set up the listener when the camera becomes available (free) so that you can use it immediately.
CameraManager.AvailabilityCallback availabilityCallback = new CameraManager.AvailabilityCallback() { @Override public void onCameraAvailable(String cameraId) {
Using the abstract class CameraManager.AvailabilityCallback is when you instantiate an object, you create an anonymous instance that implements callbacks like onCameraAvailabe() , which is actually the listener that you get from the Observer camera.
Answer:. If you put your camera’s processing commands inside the onCameraAvailable() callback, I guarantee that you would not get the error you showed.
If the camera is used by other applications in the background, this means that these other applications look bad, because the camera needs a foreground preview; they do not release the camera even after they are made with it. No one is using the camera in the background. Even so, you should not kill the camera process either, because a memory leak may occur. Because you do not know how other processes use the camera.
Uddhav Gautam
source share