How to detect no of camera's available in android device? and also i开发者_如何学编程f the device has front camera how to use it?
What I would suggest is similar to doc_180's answer, but should be able to detect both front and back facing cameras even for Froyo, though if I'm not mistaken, Froyo never supported front-facing cameras, so you'll always get a false response for frontCam
on Froyo.
PackageManager pm = getPackageManager();
boolean frontCam, rearCam;
//Must have a targetSdk >= 9 defined in the AndroidManifest
frontCam = pm.hasSystemFeature(PackageManager.FEATURE_CAMERA_FRONT);
rearCam = pm.hasSystemFeature(PackageManager.FEATURE_CAMERA);
EDIT: Just realized this is a really, really old question. Oh well, hopefully it helps someone in the future.
Use packagemanager to check if the device supports the Intent. In this case Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE
);
public static boolean isIntentAvailable(Context context, String action) {
final PackageManager packageManager = context.getPackageManager();
final Intent intent = new Intent(action);
List<ResolveInfo> list =
packageManager.queryIntentActivities(intent,
PackageManager.MATCH_DEFAULT_ONLY);
return list.size() > 0;
}
CameraInfo cameraInfo = new CameraInfo();
for (int i = 0; i < numberOfCameras; i++) {
Camera.getCameraInfo(i, cameraInfo);
if (cameraInfo.facing == CameraInfo.CAMERA_FACING_FRONT) {
}
}
The quickest way I've found to check if a (backfacing) camera exists is to check if Camera.open() returns null.
Camera cam = Camera.open();
if(null == cam){
//no camera exists
}
This should be available for earlier versions of android as well.
you can use this static method if you just want to know how many cameras there are: Camera.getNumberOfCameras(); (api 9)
/** Check if this device has a camera */
private boolean checkCameraHardware(Context context) {
if (context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA)){
// this device has a camera
return true;
} else {
// no camera on this device
return false;
}
}
I am using this method to get the count of the available camera
public int getCameraCount(){
CameraManager manager = (CameraManager) context.getSystemService(Context.CAMERA_SERVICE);
try {
String[] strings = manager.getCameraIdList();
return strings.length;
} catch (CameraAccessException e) {
e.printStackTrace();
return 0;
}
}
Try this, this worked for me in a Moto RAZR HD:
public static Camera open (int cameraId)
Example usage:
mCamera = Camera.open(1);
精彩评论