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

13,048

Solution 1

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.

Solution 2

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;
}

Solution 3

    CameraInfo cameraInfo = new CameraInfo();
    for (int i = 0; i < numberOfCameras; i++) {
        Camera.getCameraInfo(i, cameraInfo);
        if (cameraInfo.facing == CameraInfo.CAMERA_FACING_FRONT) {              

        }
    }
Share:
13,048

Related videos on Youtube

sureshmenon13196
Author by

sureshmenon13196

Updated on February 16, 2020

Comments

  • sureshmenon13196
    sureshmenon13196 about 4 years

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

  • sureshmenon13196
    sureshmenon13196 about 13 years
    hi, thanks for your reply but i am using android 2.2 i don't find CameraInfo class. what to do now?
  • Soumya Simanta
    Soumya Simanta about 13 years
    @suresh - Can you ask the user to tell the application if there is a front camera in the device or not ?
  • Anne
    Anne over 12 years
    Comment by user without comment privileges (profile): MediaStore.ACTION_IMAGE_CAPTURE doesn't work, you should use android.media.action.IMAGE_CAPTURE.
  • Cristian
    Cristian over 11 years
    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.
  • andr
    andr about 11 years
    how on earth does that answer the question?
  • Brad Moore
    Brad Moore almost 10 years
    This will return false for devices with no rear camera like the Nexus 7.
  • MCLLC
    MCLLC over 8 years
    Should try FEATURE_CAMERA_ANY instead.
  • Galya
    Galya about 7 years
    FEATURE_CAMERA_ANY is the right way to go, but it works only on API 17 and above.

Related