尝试打开相机时 Flutter image_picker 提供程序错误

Flutter image_picker provider error when trying to open camera

我将 image_picker 添加到我的 flutter 应用程序中,它在从图库中选取时有效,但是当我尝试从相机获取图像时出现以下错误(我正在使用 Android 11):

E/MethodChannel#plugins.flutter.io/image_picker(17622): Failed to handle method call E/MethodChannel#plugins.flutter.io/image_picker(17622): java.lang.IllegalArgumentException: Couldn't find meta-data for provider with authority <package_name>.flutter.image_provider E/MethodChannel#plugins.flutter.io/image_picker(17622): at androidx.core.content.FileProvider.parsePathStrategy(FileProvider.java:606) E/MethodChannel#plugins.flutter.io/image_picker(17622): at androidx.core.content.FileProvider.getPathStrategy(FileProvider.java:579) E/MethodChannel#plugins.flutter.io/image_picker(17622): at androidx.core.content.FileProvider.getUriForFile(FileProvider.java:417) E/MethodChannel#plugins.flutter.io/image_picker(17622): at io.flutter.plugins.imagepicker.ImagePickerDelegate.resolveFileProviderUriForFile(ImagePickerDelegate.java:160) E/MethodChannel#plugins.flutter.io/image_picker(17622): at io.flutter.plugins.imagepicker.ImagePickerDelegate.launchTakeImageWithCameraIntent(ImagePickerDelegate.java:384) E/MethodChannel#plugins.flutter.io/image_picker(17622): at io.flutter.plugins.imagepicker.ImagePickerDelegate.takeImageWithCamera(ImagePickerDelegate.java:358) E/MethodChannel#plugins.flutter.io/image_picker(17622): at io.flutter.plugins.imagepicker.ImagePickerPlugin.onMethodCall(ImagePickerPlugin.java:300) E/MethodChannel#plugins.flutter.io/image_picker(17622): at io.flutter.plugin.common.MethodChannel$IncomingMethodCallHandler.onMessage(MethodChannel.java:233) E/MethodChannel#plugins.flutter.io/image_picker(17622): at io.flutter.embedding.engine.dart.DartMessenger.handleMessageFromDart(DartMessenger.java:85) E/MethodChannel#plugins.flutter.io/image_picker(17622): at io.flutter.embedding.engine.FlutterJNI.handlePlatformMessage(FlutterJNI.java:818) E/MethodChannel#plugins.flutter.io/image_picker(17622): at android.os.MessageQueue.nativePollOnce(Native Method) E/MethodChannel#plugins.flutter.io/image_picker(17622): at android.os.MessageQueue.next(MessageQueue.java:335) E/MethodChannel#plugins.flutter.io/image_picker(17622): at android.os.Looper.loop(Looper.java:206) E/MethodChannel#plugins.flutter.io/image_picker(17622): at android.app.ActivityThread.main(ActivityThread.java:8512) E/MethodChannel#plugins.flutter.io/image_picker(17622): at java.lang.reflect.Method.invoke(Native Method) E/MethodChannel#plugins.flutter.io/image_picker(17622): at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:602) E/MethodChannel#plugins.flutter.io/image_picker(17622): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1130)

这是我的代码:


  File _image;

  ImagePicker picker = ImagePicker();

  Future pickImage(ImageSource source) async {
    print("picking image");
    try {
      final pickedFile = await picker.getImage(
          source: source,
          preferredCameraDevice: CameraDevice.rear,
          );

      
      if (pickedFile != null) {
        setState(() {
          _image = File(pickedFile.path);
        });
      } else {
        return null;
      }
    } catch (e) {
      print("error");
    }
  }

 .
 .
 .

 onTap: () {
     var source = ImageSource.gallery;
   
     pickImage(source);
   },
 .
 .
 .
  onTap: () {
     var source = ImageSource.camera;
    
     pickImage(source);
   },

当我将提供程序添加到 andoridmanifest 时出现另一个错误。

E/MethodChannel#plugins.flutter.io/image_picker(14565): Failed to handle method call E/MethodChannel#plugins.flutter.io/image_picker(14565): java.lang.IllegalArgumentException: Failed to find configured root that contains /data/data/<package_name>/cache/91958bf6-db2c-46d3-bf0c-b31c3dd0fc1c2799268083033056301.jpg E/MethodChannel#plugins.flutter.io/image_picker(14565): at androidx.core.content.FileProvider$SimplePathStrategy.getUriForFile(FileProvider.java:744) E/MethodChannel#plugins.flutter.io/image_picker(14565): at androidx.core.content.FileProvider.getUriForFile(FileProvider.java:418) E/MethodChannel#plugins.flutter.io/image_picker(14565): at io.flutter.plugins.imagepicker.ImagePickerDelegate.resolveFileProviderUriForFile(ImagePickerDelegate.java:160) E/MethodChannel#plugins.flutter.io/image_picker(14565): at io.flutter.plugins.imagepicker.ImagePickerDelegate.launchTakeImageWithCameraIntent(ImagePickerDelegate.java:384) E/MethodChannel#plugins.flutter.io/image_picker(14565): at io.flutter.plugins.imagepicker.ImagePickerDelegate.takeImageWithCamera(ImagePickerDelegate.java:358) E/MethodChannel#plugins.flutter.io/image_picker(14565): at io.flutter.plugins.imagepicker.ImagePickerPlugin.onMethodCall(ImagePickerPlugin.java:300) E/MethodChannel#plugins.flutter.io/image_picker(14565): at io.flutter.plugin.common.MethodChannel$IncomingMethodCallHandler.onMessage(MethodChannel.java:233) E/MethodChannel#plugins.flutter.io/image_picker(14565): at io.flutter.embedding.engine.dart.DartMessenger.handleMessageFromDart(DartMessenger.java:85) E/MethodChannel#plugins.flutter.io/image_picker(14565): at io.flutter.embedding.engine.FlutterJNI.handlePlatformMessage(FlutterJNI.java:818) E/MethodChannel#plugins.flutter.io/image_picker(14565): at android.os.MessageQueue.nativePollOnce(Native Method) E/MethodChannel#plugins.flutter.io/image_picker(14565): at android.os.MessageQueue.next(MessageQueue.java:335) E/MethodChannel#plugins.flutter.io/image_picker(14565): at android.os.Looper.loop(Looper.java:206) E/MethodChannel#plugins.flutter.io/image_picker(14565): at android.app.ActivityThread.main(ActivityThread.java:8512) E/MethodChannel#plugins.flutter.io/image_picker(14565): at java.lang.reflect.Method.invoke(Native Method) E/MethodChannel#plugins.flutter.io/image_picker(14565): at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:602) E/MethodChannel#plugins.flutter.io/image_picker(14565): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1130)

通过将 <cache-path name="cache" path="."/> 添加到 @xml/filepaths

来修复