84

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?

BryanH
  • 5,826
  • 3
  • 34
  • 47
mark
  • 1,697
  • 3
  • 14
  • 13
  • 1
    I believe (but I'm not 100% sure) that all past and present versions of the [Android Compatibility Definition Document](http://source.android.com/compatibility/) 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 Mar 10 '11 at 21:45
  • 2
    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 Apr 03 '13 at 03:41
  • Just to add that there's also devices that only have a front-facing camera. – The Berga Dec 15 '16 at 14:55
  • 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 Nov 15 '18 at 09:16

15 Answers15

228

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

dpjanes
  • 5,745
  • 3
  • 25
  • 30
  • 4
    so quick question. this might be dumb but i am new to android/java where do you get context from???? thanks – grobartn Dec 11 '11 at 00:09
  • 3
    @grobartn: `public class YourActivity extends Activity { Context contextActivity; PackageManager pm; @Override public void onCreate(Bundle savedInstanceState) { ... contextActivity = this; pm = contextActivity.getPackageManager(); ...` – Klofi Dec 11 '11 at 01:08
  • 14
    From API level 9, you may want to check for FEATURE_CAMERA_FRONT besides FEATURE_CAMERA. For example, Nexus 7 (which has only one frontal camera) returns false to FEATURE_CAMERA. – Jorge Cevallos Sep 07 '12 at 00:00
  • 6
    For Kindle Fire HD 7" (Android 4.0.3), I found that it reports FEATURE_CAMERA, even though it only has a front camera! Awful ... as a workaround I also test (FEATURE_CAMERA_FRONT and Camera.getNumberOfCameras() == 1) then I know there is no rear camera. – CSmith Jan 03 '13 at 19:59
  • 6
    After API level 17, you can use PackageManager.FEATURE_CAMERA_ANY – X.Y. Jun 19 '14 at 22:04
  • This answer doesn't work for me with KitKat through the emulator at least (I don't have a KitKat device without a camera to test). I have the emulator set for no front and no back camera but this tells me there is a camera. The answer below which counts the number of cameras does work though -- it returns 0. – GregInWI2 Oct 31 '14 at 14:05
43

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.

Eddy Talvala
  • 17,243
  • 2
  • 42
  • 47
  • 2
    this worked for me and the FEATURE_CAMERA did not in API 19 – sobelito Sep 08 '16 at 14:17
  • One line version : boolean hasCam = android.hardware.Camera.getNumberOfCameras() > 0; – J.Jacobs Dec 14 '16 at 09:59
  • This answer is better. hasSystemFeature(PackageManager.FEATURE_CAMERA) returns false on Kit-Kat (Nexus 7) – Kirill Karmazin Mar 16 '17 at 12:19
  • 1
    It returns false for Nexus 7 (2013), because FEATURE_CAMERA is only about back-facing cameras (due to backwards compatibility); FEATURE_CAMERA_ANY is the feature that is true if there's at least one camera of any sort on the device. – Eddy Talvala Mar 16 '17 at 23:10
  • Why is `android.hardware.Camera` deprecated? What should be used instead for this function? I know we now have `android.hardware.camera2` , but where can we find the alternative to `getNumberOfCameras` there? – android developer Nov 15 '18 at 09:21
12

you should use this to find camera in your device

public static boolean isCameraAvailable(Context context) {
    return context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_ANY);
}
TameHog
  • 950
  • 11
  • 23
Vishwa
  • 251
  • 3
  • 6
  • 1
    I think this is the most useful one. FEATURE_CAMERA_ANY is a good choice. Here is a shorter version of the code for those who prefer it. `public static boolean isCameraAvailable(Context context) { return context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_ANY)) }` – miva2 Jan 26 '15 at 09:23
7

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();
}
Frank
  • 12,010
  • 8
  • 61
  • 78
5

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

Nurul Akter Towhid
  • 3,046
  • 2
  • 33
  • 35
3

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;
        }
    }
DjP
  • 4,537
  • 2
  • 25
  • 34
2

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");
          }
Mr T
  • 1,409
  • 1
  • 17
  • 24
2

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

samaniego
  • 169
  • 2
  • 9
2

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

Googlian
  • 6,077
  • 3
  • 38
  • 44
2

it is better to check ANY camera on the device since it could be external camera as well

packageManager.hasSystemFeature(FEATURE_CAMERA_ANY)

Documentation:

Feature for getSystemAvailableFeatures and hasSystemFeature: The device has at least one camera pointing in some direction, or can support an external camera being connected to it.

Zafer Celaloglu
  • 1,438
  • 1
  • 17
  • 28
1

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)

mabeiyi
  • 357
  • 2
  • 5
  • 14
0

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)
Srikar Reddy
  • 3,650
  • 4
  • 36
  • 58
0

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

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.

Henrique Monte
  • 1,272
  • 1
  • 15
  • 22
-45

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.

user197385
  • 54
  • 2