15

How to detect no of camera's available in android device? and also if the device has front camera how to use it?

sureshmenon13196
  • 151
  • 1
  • 1
  • 4

8 Answers8

41

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.

Kevin Coppock
  • 133,643
  • 45
  • 263
  • 274
  • 2
    Keep 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
5

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;
}
Anne
  • 26,765
  • 9
  • 65
  • 71
uncaught_exceptions
  • 21,712
  • 4
  • 41
  • 48
  • 2
    Comment 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
2
    CameraInfo cameraInfo = new CameraInfo();
    for (int i = 0; i < numberOfCameras; i++) {
        Camera.getCameraInfo(i, cameraInfo);
        if (cameraInfo.facing == CameraInfo.CAMERA_FACING_FRONT) {              

        }
    }
Soumya Simanta
  • 11,523
  • 24
  • 106
  • 161
0

you can use this static method if you just want to know how many cameras there are: Camera.getNumberOfCameras(); (api 9)

breenger
  • 649
  • 4
  • 6
0
/** 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;
    }
}
rsc
  • 10,348
  • 5
  • 39
  • 36
0

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;
    }
}
MRX
  • 1,400
  • 3
  • 12
  • 32
0

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.

Andrew T.
  • 2,088
  • 1
  • 20
  • 42
-1

Try this, this worked for me in a Moto RAZR HD:

public static Camera open (int cameraId)

Example usage:

mCamera = Camera.open(1);
andr
  • 15,970
  • 10
  • 45
  • 59