Android: Determining number of cameras on 2.2 and

2019-07-15 13:34发布

问题:

My app needs to run on Android 2.2 and above. I need a way to determine the number of cameras available. There are a number of posts addressing this but I couldn't find one that works.

One solution is simply to detect the OS version and anything with 2.2 would only be limited to 1 camera (even if the device really did have 2) but I still have no way of figuring out the number of cameras for any versions greater than 2.2.

回答1:

Multiple cameras are not supported on earlier OS versions, so you can just assume you have 1. even the device had 2, they are not supported by the standard Android API. For newer OS versions, just use the Camera.getNumberOfCameras() and Camera.getCameraInfo() with an API level guard statement:

 int numCameras = 1;
 if (Build.Version.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) {
   numCameras = Camera.getNumberOfCameras();
 }