我想在尝试打开二维码阅读器之前检查设备是否有摄像头。
我有以下代码:
public boolean checkDeviceCompatibility() {
PackageManager pm = context.getPackageManager();
if (pm.hasSystemFeature(PackageManager.FEATURE_CAMERA_ANY)) {
if (pm.hasSystemFeature(PackageManager.FEATURE_CAMERA)) {
return true;
} else {
// use front camera
Toast.makeText(
context,
"This device does not have a back camera so it has automatically reverted to the front camera",
Toast.LENGTH_SHORT).show();
return true;
}
} else {
util.displayErrorDialog(
context,
"This device does not have any cameras and therefore cannot make use of the QR Code feature.");
return false;
}
}
但是现在,如果我在带有两个摄像头的 galaxy S3 上以 Debug模式运行这段代码。第一个 if 语句返回 false。
为什么会这样?
最佳答案
FEATURE_CAMERA_ANY
was added in Android 4.2. hasSystemFeature(PackageManager.FEATURE_CAMERA_ANY)
应为任何 4.2 之前的设备返回 false
。如果您的 S3 仍在 4.1 上,那就可以解释您的问题。
关于android - pm.hasSystemFeature(PackageManager.FEATURE_CAMERA_ANY) 没有给出正确的答案,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19458342/