Check if device has a camera?

前端 未结 14 1182
粉色の甜心
粉色の甜心 2020-11-30 21:59

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

相关标签:
14条回答
  • 2020-11-30 22:07

    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

    0 讨论(0)
  • 2020-11-30 22:08

    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.

    0 讨论(0)
  • 2020-11-30 22:10

    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 讨论(0)
  • 2020-11-30 22:12

    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;
            }
        }
    
    0 讨论(0)
  • 2020-11-30 22:14

    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.

    0 讨论(0)
  • 2020-11-30 22:14

    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

    0 讨论(0)
提交回复
热议问题