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 m
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
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.
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)
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;
}
}
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.
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