How to detect no of camera's available in android device? and also if the device has front camera how to use it?
8 Answers
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.

- 133,643
- 45
- 263
- 274
-
2Keep in mind that you can compile your app using Android 2.3 or higher and still make it compatible with Android 2.2. Remember that constants like `PackageManager.FEATURE_CAMERA_FRONT` are actually hardcoded by the compiler, so it will not fail at runtime on Android 2.2 or older. – Cristian Aug 05 '12 at 04:01
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;
}

- 26,765
- 9
- 65
- 71

- 21,712
- 4
- 41
- 48
-
2Comment by user without comment privileges ([profile](http://stackoverflow.com/users/1005899/cfg)): `MediaStore.ACTION_IMAGE_CAPTURE` doesn't work, you should use `android.media.action.IMAGE_CAPTURE`. – Anne Dec 01 '11 at 20:36
CameraInfo cameraInfo = new CameraInfo();
for (int i = 0; i < numberOfCameras; i++) {
Camera.getCameraInfo(i, cameraInfo);
if (cameraInfo.facing == CameraInfo.CAMERA_FACING_FRONT) {
}
}

- 11,523
- 24
- 106
- 161
-
hi, thanks for your reply but i am using android 2.2 i don't find CameraInfo class. what to do now? – sureshmenon13196 Apr 08 '11 at 04:58
-
@suresh - Can you ask the user to tell the application if there is a front camera in the device or not ? – Soumya Simanta Apr 08 '11 at 20:13
you can use this static method if you just want to know how many cameras there are: Camera.getNumberOfCameras(); (api 9)

- 649
- 4
- 6
/** 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;
}
}

- 10,348
- 5
- 39
- 36
-
This will return false for devices with no rear camera like the Nexus 7. – Brad Moore May 28 '14 at 10:33
-
1
-
FEATURE_CAMERA_ANY is the right way to go, but it works only on API 17 and above. – Galya Mar 23 '17 at 12:27
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;
}
}

- 1,400
- 3
- 12
- 32
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.

- 2,088
- 1
- 20
- 42
Try this, this worked for me in a Moto RAZR HD:
public static Camera open (int cameraId)
Example usage:
mCamera = Camera.open(1);