How to determine if a camera is being used?

Is there a way to find if Android Camera is used in code?

+7
source share
4 answers

Is there any way to find if the camera is Android?

Yes, Camera.open() will give you an exception if the camera is in use.

From docs ,

 /** A safe way to get an instance of the Camera object. */ public static Camera getCameraInstance(){ Camera c = null; try { c = Camera.open(); // attempt to get a Camera instance } catch (Exception e){ // Camera is not available (in use or does not exist) } return c; // returns null if camera is unavailable } 
+4
source

I do not know why this question is asked several times as soon as you start your own activity or the application camera itself will be released, since the work performed for the camera will be paused.

+2
source

You can try this method. If it returns true, then the camera is still used by some applications.

 public boolean isCameraUsebyApp() { Camera camera = null; try { camera = Camera.open(); } catch (RuntimeException e) { return true; } finally { if (camera != null) camera.release(); } return false; } 

Then show the toast to the user to reboot the device, as the camera must restart.

+2
source

I know this is a really old question, but I stumbled upon it with a google search thinking the same thing. With newer versions of android, you can register CameraManager.AvailabilityCallback to determine if the camera is in use or not. Here is a sample code:

 import android.hardware.camera2.CameraManager; // within constructor // Figure out if Camera is Available or Not CameraManager cam_manager = (CameraManager) mContext.getSystemService(Context.CAMERA_SERVICE); cam_manager.registerAvailabilityCallback(camAvailCallback, mHandler); CameraManager.AvailabilityCallback camAvailCallback = new CameraManager.AvailabilityCallback() { public void onCameraAvailable(String cameraId) { cameraInUse=false; Log.d(TAG, "notified that camera is not in use."); } public void onCameraUnavailable(String cameraId) { cameraInUse=true; Log.d(TAG, "notified that camera is in use."); } }; 
+1
source

All Articles