Check if device has a camera?
Asked Answered
G

15

86

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?

Gertie answered 22/12, 2009 at 4:9 Comment(4)
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).Druce
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'.Boren
Just to add that there's also devices that only have a front-facing camera.Zipangu
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?Cornfield
I
-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.

Inattention answered 22/12, 2009 at 12:17 Comment(7)
And don't forget to release the Camera object!Charitacharitable
See dpjanes answer below for the correct way to see if a device has a camera.Skimmer
Bad approach. Expensive call. If the same camera is opened by other applications, this will throw a RuntimeException. Doesn't suit purpose.Tuppence
This answer has become so famous! :) meta.stackoverflow.com/a/316273/651647Splanchnic
This implementation on Android 6.0 (API Level 23) + would throw a SecurityException unless you had specifically requested the "android.permission.CAMERA" permission at runtime.Utterance
This answer solved my issue with rooted Android TV box. The ones telling me to use package manager does not.Flynn
This would work but It is bad practice to use exceptions to determine code logic flow.Wilton
M
230

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

Musso answered 10/3, 2011 at 21:31 Comment(6)
so quick question. this might be dumb but i am new to android/java where do you get context from???? thanksSayed
@grobartn: public class YourActivity extends Activity { Context contextActivity; PackageManager pm; @Override public void onCreate(Bundle savedInstanceState) { ... contextActivity = this; pm = contextActivity.getPackageManager(); ...Barozzi
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.Feign
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.Bysshe
After API level 17, you can use PackageManager.FEATURE_CAMERA_ANYSordino
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.Coachman
A
44

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.

Allerie answered 15/5, 2012 at 1:26 Comment(5)
this worked for me and the FEATURE_CAMERA did not in API 19Klemperer
One line version : boolean hasCam = android.hardware.Camera.getNumberOfCameras() > 0;Latoshalatouche
This answer is better. hasSystemFeature(PackageManager.FEATURE_CAMERA) returns false on Kit-Kat (Nexus 7)Gauntlet
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.Allerie
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?Cornfield
L
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);
}
Leahy answered 17/1, 2015 at 11:37 Comment(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)) }Trefoil
P
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();
}
Peck answered 6/10, 2015 at 14:47 Comment(0)
B
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

Barrington answered 21/6, 2016 at 14:33 Comment(0)
P
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;
        }
    }
Photoplay answered 13/4, 2016 at 7:43 Comment(0)
C
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");
          }
Clothes answered 21/10, 2014 at 5:19 Comment(0)
C
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

Cobelligerent answered 4/12, 2015 at 0:31 Comment(0)
P
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

Procambium answered 20/9, 2019 at 5:52 Comment(0)
S
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.

Susurrus answered 15/11, 2021 at 20:13 Comment(0)
C
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)

Cardiology answered 28/2, 2012 at 12:11 Comment(0)
U
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)
Unadvised answered 20/10, 2018 at 8:42 Comment(0)
B
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_;
    }
Buffy answered 23/11, 2018 at 5:39 Comment(0)
P
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.

Petite answered 27/3, 2019 at 17:7 Comment(0)
I
-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.

Inattention answered 22/12, 2009 at 12:17 Comment(7)
And don't forget to release the Camera object!Charitacharitable
See dpjanes answer below for the correct way to see if a device has a camera.Skimmer
Bad approach. Expensive call. If the same camera is opened by other applications, this will throw a RuntimeException. Doesn't suit purpose.Tuppence
This answer has become so famous! :) meta.stackoverflow.com/a/316273/651647Splanchnic
This implementation on Android 6.0 (API Level 23) + would throw a SecurityException unless you had specifically requested the "android.permission.CAMERA" permission at runtime.Utterance
This answer solved my issue with rooted Android TV box. The ones telling me to use package manager does not.Flynn
This would work but It is bad practice to use exceptions to determine code logic flow.Wilton

© 2022 - 2024 — McMap. All rights reserved.