intent.resolveActivity() 在带摄像头的设备上为 NULL (4.2.2)

intent.resolveActivity() is NULL on a device with the camera (4.2.2)

使用此 piece of code is suggested 并说明

Notice that the startActivityForResult() method is protected by a condition that calls resolveActivity(), which returns the first activity component that can handle the intent. Performing this check is important because if you call startActivityForResult() using an intent that no app can handle, your app will crash. So as long as the result is not null, it's safe to use the intent.

代码:

private void dispatchTakePictureIntent() {
    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
        startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
    }
}

它工作正常,但不适用于阿尔卡特设备 运行 4.2.2。我在 toast "Cannot use camera..."

中收到 ELSE 消息

如果设备有摄像头,这怎么可能?

我现在该怎么办?如何处理这个用例 - 显然我必须依赖一些带摄像头的设备会报告 "cannot use camera",检测到这些设备有摄像头然后使用它?!

文档或 SO 问题未提及 ELSE 案例。

How is this possible if a device has the camera?

该设备上预装的相机应用程序的作者未能支持 ACTION_IMAGE_CAPTURE Intent 操作。有lots of bugs with camera apps.

虽然可能与您的情况无关,但请记住,在 Android 4.3+ 上使用受限配置文件,设备的当前用户可能无法访问 ACTION_IMAGE_CAPTURE activity,即使设备恰好有一个。

What should I do now?

做类似的事情:

  • 告诉用户他们没有兼容的相机应用程序,并引导他们下载一个您知道可以与您的应用程序配合使用的应用程序

  • 不要依赖ACTION_IMAGE_CAPTURE,而是在您的应用中嵌入拍照功能

  • resolveActivity() returns null

    [=39 的设备上禁用需要 ACTION_IMAGE_CAPTURE 的应用功能=]

请检查您在AndroidManifest.xml

中是否有使用权限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

如果你使用然后使用这个编辑的代码来检查是否有摄像头..

private void dispatchTakePictureIntent() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    if (getApplicationContext().getPackageManager().hasSystemFeature(
            PackageManager.FEATURE_CAMERA)) {
        // this device has a camera
        startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
    } else {
        // no camera on this device
    }

}

如果您想使用以前的代码,请不要忘记在 AndroidManifest.xml

中设置此代码
    <uses-feature android:name="android.hardware.camera"
              android:required="true" />