Camera error “Cannot connect to the camera” or the message “Camera is being used by another application” appears on some phones,

I made a sound recording in the background using Android MediaRecorder , if the sound is being recorded and the user opens his own camera for video recording, it gives

Camera error "Cannot connect to camera"

or on some phones the error appears as

Your camera is being used by another application.

enter image description here

If I stop the mediarecorder , then the camera’s own video recording is beautiful, I searched for events to know when the camera will start the video, and then in my application stop the media recorder, I found that the BroadcastReceiver with filters

<receiver android:name=".receiver.CameraReceiver"> <intent-filter android:priority="10000"> <action android:name="android.Medintent.action.CAMERA_BUTTON" /> <action android:name="android.hardware.action.NEW_PICTURE" /> <action android:name="android.hardware.action.NEW_VIDEO" /> <category android:name="android.intent.category.DEFAULT" /> <data android:mimeType="image/*" /> <data android:mimeType="video/*" /> </intent-filter> </receiver> 

NEW_VIDEO and NEW_PICTURE events captured when capturing and saving an image or video in a directory. Does anyone know how to solve this problem? I want to know in my application the event when Native / Camera Apps is about to record video. thanks in advance

+8
android video-capture camera android-camera mediarecorder
source share
2 answers

Even I had the same problem. When a camera resource is used by an application until it is released, you can use them in another application or even in a service. If any service uses the camera’s resource until it releases the same, we won’t be able to use the camera’s hardware. You can double check if the camera hardware is used with this code: -

  private boolean isCameraInUse() { Log.v(TAG, "isCameraInUse()"); boolean isCameraInUse = false; if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)//Use Camera Api for Version Code < 23 and mCamera manager above it. { String cameraId = null; 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)); isCameraInUse = true; } try { camManager.setTorchMode(cameraId, true); camManager.setTorchMode(cameraId, false); } catch (CameraAccessException e) { Log.e(TAG, Log.getStackTraceString(e)); isCameraInUse = true; } } else { Camera c = null; try { c = Camera.open(); } catch (RuntimeException e) { Log.e(TAG, Log.getStackTraceString(e)); turnFlashOff(mContext); isCameraInUse = true; } finally { if (c != null) c.release(); } } return isCameraInUse; } 
0
source share

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) { // your further stuffs. You must put all of your camera related logic here. } public void onCameraUnavailable(String cameraId) { //you can logcat camera not available } }; 

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.

0
source share

All Articles