82
votes

In my app, I'd like to use the camera, if the device has one. Are there any devices running android that do not have a camera? By including the following into my manifest:

<uses-feature android:name="android.hardware.camera" android:required="false"/>

then it's basically saying "I'll use a camera if one exists, but don't need one to run the app".

How could I check if a camera exists on the device, before attempting to use the Camera class?

14
I believe (but I'm not 100% sure) that all past and present versions of the Android Compatibility Definition Document specify a camera with some minimum resolution. Complying with the CDD is a prerequisite for licensed access to the Android Market and proprietary Google applications. There are Android devices out there that don't, though (e.g. the Nook color). - ephemient
As of today, the current version of the document (4.2) specifies "Device implementations SHOULD include a rear-facing camera, and MAY include a front-facing camera." Note that it does not use the keyword 'MUST'. - Matthew Blackford
Just to add that there's also devices that only have a front-facing camera. - The Berga
Is there a point in adding this to the manifest, if it says that the app should work with and without a camera? Does it affect the app on the Play Store, perhaps? - android developer

14 Answers

225
votes

This is what I'm using

import android.content.pm.PackageManager;

PackageManager pm = context.getPackageManager();

if (pm.hasSystemFeature(PackageManager.FEATURE_CAMERA)) {
}

All sorts of other fun things to test for are available too - the compass, is location available, is there a front facing camera: http://developer.android.com/reference/android/content/pm/PackageManager.html

43
votes

To find out how many cameras are available on your device, you can call:

import android.hardware.Camera;

int numCameras = Camera.getNumberOfCameras();
if (numCameras > 0) {
  hasCamera = true;
}

Camera.getNumberOfCameras() is static, so it doesn't require actually connecting to a camera. This works since API 9.

Edit:

With the newer camera2 API, you can also call CameraManager.getCameraIdList(), which gives a list of the all the valid camera IDs, instead of just the count.

12
votes

you should use this to find camera in your device

public static boolean isCameraAvailable(Context context) {
    return context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_ANY);
}
6
votes

Camera.getNumberOfCameras() is deprecated. You can use:

@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public int getNumberOfCameras() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        try {
            return ((CameraManager) getSystemService(Context.CAMERA_SERVICE)).getCameraIdList().length;
        } catch (CameraAccessException e) {
            Log.e("", "", e);
        }
    }
    return Camera.getNumberOfCameras();
}
5
votes

Use the PackageManager.hasSystemFeature() method for checking 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;
    }
}

Source: https://developer.android.com/guide/topics/media/camera.html#custom-camera

3
votes

by following way we can check does device has camera or not.

/** Check if this device has a camera */
    public static boolean checkCameraHardware(Context context) {
        if (context.getPackageManager().hasSystemFeature(
                PackageManager.FEATURE_CAMERA)) 
        {
            return true;
        }
        else if(context.getPackageManager().hasSystemFeature(
                PackageManager.FEATURE_CAMERA_FRONT))
        {
            return true;
        }
        else {
            return false;
        }
    }
2
votes

Try this :

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

from : http://developer.android.com/guide/topics/media/camera.html

2
votes

As per Android documentation :

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

Refer more about the camera API :
https://developer.android.com/guide/topics/media/camera.html#detect-camera

1
votes

If you are using Android 2.3, there are some APIs that you can check your camera status, such as the number of cameras (front and back)

1
votes

try this

For front camera

    Context context = this;
    PackageManager packageManager = context.getPackageManager();
    if (packageManager.hasSystemFeature(PackageManager.FEATURE_CAMERA_FRONT)) {

        Utils.makeAlertDialog(context, "Has Front Camera ?", "YES");

    } else {

        Utils.makeAlertDialog(context, "Has Front Camera ?", "NO");
          }

for back camera

    Context context = this;
    PackageManager packageManager = context.getPackageManager();
    if (packageManager.hasSystemFeature(PackageManager.FEATURE_CAMERA)) {

        Utils.makeAlertDialog(context, "Has back Camera ?", "YES");

    } else {

        Utils.makeAlertDialog(context, "Has back Camera ?", "NO");
          }
0
votes

As per the documentation, you have to use Package Manager to check if Camera is available on the device or not

In Java:

final boolean isCameraAvailable = getPackageManager().hasSystemFeature(FEATURE_CAMERA);

In Kotlin:

val isCameraAvailable = packageManager.hasSystemFeature(FEATURE_CAMERA)
0
votes

I found in android tv boxes where you can plug and play usb camera a number of times. At some point of time, The camera service starts saying that it detected one camera in the system while no camera is connected to the system. This happens when you plug in/out the camera a number of times. To fix that, I found this solution working:

//under oncreate:
//cameraManager = ((CameraManager) getSystemService(Context.CAMERA_SERVICE)); 

public int getNumberOfCameras() {
        int count_ = 0;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            try {
                count_ = cameraManager.getCameraIdList().length;

                if(count_==1)
                {
                    try {
                        cameraManager.getCameraCharacteristics(cameraManager.getCameraIdList()[0]);
                    }catch (Exception e)
                    {
                        count_ = 0;
                    }
                }

            } catch (Exception e) {
               //e.printStackTrace();
            }
        }
        else {
            count_ = Camera.getNumberOfCameras();
        }

        return count_;
    }
0
votes

One line solution:

public static boolean hasCamera(Context context) {
    return context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA);
}

Put this method in your Utils.java project class.

-45
votes

I've not tried it, but:

private android.hardware.Camera mCameraDevice;

try {
  mCameraDevice = android.hardware.Camera.open();
} catch (RuntimeException e) {
  Log.e(TAG, "fail to connect Camera", e);
  // Throw exception
}

May be what you need.