Check if device has a camera?

前端 未结 14 1192
粉色の甜心
粉色の甜心 2020-11-30 21:59

In my app, I\'d like to use the camera, if the device has one. Are there any devices running android that do not have a camera? By including the following into my m

14条回答
  •  南方客
    南方客 (楼主)
    2020-11-30 22:15

    To find out how many cameras are available on your device, you can call:

    import android.hardware.Camera;
    
    int numCameras = Camera.getNumberOfCameras();
    if (numCameras > 0) {
      hasCamera = true;
    }
    

    Camera.getNumberOfCameras() is static, so it doesn't require actually connecting to a camera. This works since API 9.

    Edit:

    With the newer camera2 API, you can also call CameraManager.getCameraIdList(), which gives a list of the all the valid camera IDs, instead of just the count.

提交回复
热议问题