Check if device has a camera?

AndroidAndroid Camera

Android Problem Overview


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?

Android Solutions


Solution 1 - Android

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

Solution 2 - Android

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.

Solution 3 - Android

you should use this to find camera in your device

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

Solution 4 - Android

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

Solution 5 - Android

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

Solution 6 - Android

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

Solution 7 - Android

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

Solution 8 - Android

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

Solution 9 - Android

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

Solution 10 - Android

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)

Solution 11 - Android

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.

Solution 12 - Android

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)

Solution 13 - Android

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

Solution 14 - Android

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.

Solution 15 - Android

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionmarkView Question on Stackoverflow
Solution 1 - AndroiddpjanesView Answer on Stackoverflow
Solution 2 - AndroidEddy TalvalaView Answer on Stackoverflow
Solution 3 - AndroidVishwaView Answer on Stackoverflow
Solution 4 - AndroidFrankView Answer on Stackoverflow
Solution 5 - AndroidNurul Akter TowhidView Answer on Stackoverflow
Solution 6 - AndroidDjPView Answer on Stackoverflow
Solution 7 - AndroidMr TView Answer on Stackoverflow
Solution 8 - AndroidsamaniegoView Answer on Stackoverflow
Solution 9 - AndroidGooglianView Answer on Stackoverflow
Solution 10 - AndroidmabeiyiView Answer on Stackoverflow
Solution 11 - AndroidZafer CelalogluView Answer on Stackoverflow
Solution 12 - AndroidSrikar ReddyView Answer on Stackoverflow
Solution 13 - AndroidSourja BanerjeeView Answer on Stackoverflow
Solution 14 - AndroidHenrique MonteView Answer on Stackoverflow
Solution 15 - Androiduser197385View Answer on Stackoverflow