Flutter : PlatformException(no_available_camera, 没有可用于拍照的相机。, null, null)

Flutter : PlatformException(no_available_camera, No cameras available for taking pictures., null, null)

伙计们!我面对的是这个相机版本。它发生在我合并​​我的项目之后。它说没有可用的相机,但当时 运行 没问题。请看!

E/flutter: [ERROR:flutter/lib/ui/ui_dart_state.cc(177)] Unhandled Exception: PlatformException(no_available_camera, No cameras available for taking pictures., null, null)
    #0      StandardMethodCodec.decodeEnvelope (package:flutter/src/services/message_codecs.dart:582:7)
    #1      MethodChannel._invokeMethod (package:flutter/src/services/platform_channel.dart:159:18)
    <asynchronous suspension>
    #2      MethodChannel.invokeMethod (package:flutter/src/services/platform_channel.dart:332:12)
    #3      MethodChannelImagePicker.pickImagePath (package:image_picker_platform_interface/src/method_channel/method_channel_image_picker.dart:62:21)
    #4      MethodChannelImagePicker.pickImage (package:image_picker_platform_interface/src/method_channel/method_channel_image_picker.dart:30:25)
    #5      ImagePicker.getImage (package:image_picker/image_picker.dart:101:21)
    #6      _ImageEvaluationState.getImage (package:PhotoEarn/screens/camera/image_evaluation.dart:201:38)
    #7      _ImageEvaluationState.build.<anonymous closure> (package:PhotoEarn/screens/camera/image_evaluation.dart:1099:33)
    #8      _InkResponseState._handleTap (package:flutter/src/material/ink_well.dart:993:19)
    #9      _InkResponseState.build.<anonymous closure> (package:flutter/src/material/ink_well.dart:1111:38)
    #10     GestureRecognizer.invokeCallback (package:flutter/src/gestures/recognizer.dart:183:24)
    #11     TapGestureRecognizer.handleTapUp (package:flutter/src/gestures/tap.dart:598:11)
    #12     BaseTapGestureRecognizer._checkUp (package:flutter/src/gestures/tap.dart:287:5)
    #13     BaseTapGestureRecognizer.acceptGesture (package:flutter/src/gestures/tap.dart:259:7)
    #14     GestureArenaManager.sweep (package:flutter/src/gestures/arena.dart:157:27)
    #15     GestureBinding.handleEvent (package:flutter/src/gestures/binding.dart:362:20)
    #16     GestureBinding.dispatchEvent (package:flutter/src/gestures/binding.dart:338:22)
    #17     RendererBinding.dispatchEvent (package:flutter/src/rendering/binding.dart:267:11)
    #18     GestureBinding._handlePointerEvent (package:flutter/src/gestures/binding.dart:295:7)
    #19     GestureBinding._flushPointerEventQueue (package:flutter/src/gestures/binding.dart:240:7)
    #20     GestureBinding._handlePointerDataPacket (package:flutter/src/gestures/binding.dart:213:7)
    #21     _rootRunUnary (dart:async/zone.dart:1206:13)
    #22     _CustomZone.runUnary (dart:async/zone.dart:1100:19)
    #23     _CustomZone.runUnaryGuarded (dart:async/zone.dart:1005:7)
    #24     _invoke1 (dart:ui/hooks.dart:265:10)
    #25     _dispatchPointerDataPacket (dart:ui/hooks.dart:174:5)

请查看此错误消息并提供帮助。我希望收到你们所有人的来信。提前谢谢你。

如果您的 compileSdkVersiontargetSdkVersion 是 30(或以上),则将查询信息添加到您的 Android 清单文件中,直接在清单标签下:

<manifest package="your.app.package.name">
    <queries>
          <intent>
               <action android:name="android.media.action.IMAGE_CAPTURE" />
          </intent>
    </queries>
</manifest>

这应该可以解决您的问题。

鉴于目标是仅访问相机,我不确定添加 QUERY_ALL_PACKAGES 是否是正确的解决方案。如果 targetSdkVersion=30(或更高),将 <queries> 添加到您的 AndroidManifest.xml 看起来是一个更可取的解决方案 -

<manifest package="com.example">
    <queries>
        <intent>
            <action android:name="android.media.action.IMAGE_CAPTURE" />
        </intent>
    </queries>
    ...
</manifest>

我遇到了同样的问题。对于android,您需要在AndroidManifest.xml文件中添加Camera权限和Query all packages权限,如下所示:

<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.QUERY_ALL_PACKAGES"/>

如果您的目标是 android 11/ android sdk 30 或更高版本,您需要将以下权限添加到 android 清单 xml 文件。

<uses-permission android:name="android.permission.QUERY_ALL_PACKAGES"/>

它可能会再次发出警告,但您可以暂时忽略它。问候。

我在 Emulator 上测试,当我切换到 Real device 时,一切正常。