Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find whether the camera is in use?

Is there any way to find whether the Android Camera is in use in code?

like image 929
user1414146 Avatar asked Jun 27 '12 06:06

user1414146


2 Answers

Is there any way to find whether the android camera is in use?

Yes, Camera.open() will give you an Exception if Camera is in use.

From the 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
}
like image 68
COD3BOY Avatar answered Oct 30 '22 10:10

COD3BOY


I know this is a really old question, but I stumbled upon it with a google search wondering about the same thing. With the newer versions of android, you can register the CameraManager.AvailabilityCallback to determine if the camera is in use or not. Here's some example 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.");

    }
};
like image 31
PressingOnAlways Avatar answered Oct 30 '22 10:10

PressingOnAlways