androidcameraandroid-cameraandroid-5.0-lollipopcwac-camera

Best way to check if Camera exists on Android device?


There is the cwac-camera library, there is also the android.hardware.camera that was deprecated in Lollipop. Now there is android.hardware.camera2.

With andorid.hardware.camera, the solution was as simple as checking if Camera.getNumOfCameras() <= 0. I don't know the solution with android.hardware.camera2.

I do have the cwac-camera library included in my project, but I don't see any documentation that leads to a quick and simple way to check if a camera even exists on the device.

My current attempt deals with checking the SDK version. If it is less than LOLLIPOP, then I use the Camera.getNumOfCameras <= 0. Otherwise, I want to use android.hardware.camera2, but I can't find centralized doc relating to how to simply check if the phone even has cameras.


Solution

  • Try this:

        import android.content.pm.PackageManager;
        PackageManager pm = context.getPackageManager();
        if (pm.hasSystemFeature(PackageManager.FEATURE_CAMERA_ANY)) {
        bool device_has_camera = True
    }