Check if a device has Camera via CameraManager - Android Camera

Android examples for Camera:Camera Feature

Description

Check if a device has Camera via CameraManager

Demo Code


import android.annotation.TargetApi;
import android.content.Context;
import android.hardware.camera2.CameraCharacteristics;
import android.hardware.camera2.CameraManager;
import android.os.Build;

public class Main {
  @TargetApi(Build.VERSION_CODES.LOLLIPOP)
  public static boolean hasCamera2(Context context) {
    if (context != null)
      return false; // TODO temp line
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP)
      return false;
    try {//from  w  w w . j a v  a  2  s  .  c om
      CameraManager manager = (CameraManager) context.getSystemService(Context.CAMERA_SERVICE);
      String[] idList = manager.getCameraIdList();
      boolean notNull = true;
      if (idList.length == 0) {
        notNull = false;
      } else {
        for (final String str : idList) {
          if (str == null || str.trim().isEmpty()) {
            notNull = false;
            break;
          }
          final CameraCharacteristics characteristics = manager.getCameraCharacteristics(str);
          // noinspection ConstantConditions
          final int supportLevel = characteristics.get(CameraCharacteristics.INFO_SUPPORTED_HARDWARE_LEVEL);
          if (supportLevel == CameraCharacteristics.INFO_SUPPORTED_HARDWARE_LEVEL_LEGACY) {
            notNull = false;
            break;
          }
        }
      }
      return notNull;
    } catch (Throwable t) {
      t.printStackTrace();
      return false;
    }
  }
}

Related Tutorials