简体   繁体   English

颤振:“与设备的连接丢失。” 第二次使用 image_picker 从 iOS 上的图库中选择照片

[英]Flutter: 'Lost connection to device.' second time using image_picker to select photo from gallery on iOS

I am developing a mobile app in flutter that requires the user to select an image from the gallery.我正在 Flutter 中开发一个移动应用程序,它需要用户从图库中选择一个图像。

I am using image_picker: ^0.6.7+11 and here is my code:我正在使用 image_picker: ^0.6.7+11 这是我的代码:

if (await Permission.photos.request().isGranted) {
    try {
      final image =
          await ImagePicker().getImage(source: ImageSource.gallery);
      if (image != null) {
        photo.clearData();
        File _image = File(image.path);
        photo.addOriginal(_image);
      } else {
        print('no image selected');
      }
    } on PlatformException catch (e) {
      print('Platform exception $e');
    } catch (e) {
      print('Unknown error: $e');
    }
  }

In the android emulator, everything works fine.在 android 模拟器中,一切正常。 In the iOS simulator I am able to choose an image, but if I try to choose a second image then the app crashes and 'Lost connection to device.'在 iOS 模拟器中,我可以选择一个图像,但如果我尝试选择第二个图像,则应用程序会崩溃并显示“与设备的连接丢失”。 is printed in the run tab - but no errors.打印在运行选项卡中 - 但没有错误。

Question: How can I fix this so that I can go back to the gallery and select a different image as many times as I want on iOS?问题:如何解决此问题,以便我可以返回图库并在 iOS 上根据需要多次选择不同的图像?

When debugging I have come across this:调试时我遇到了这个:

PlatformException(multiple_request, Cancelled by a second request, null, null)

I have gone through as many similar questions as I can find on here, GitHub etc... I have:我已经在这里、GitHub 等上找到了尽可能多的类似问题......我有:

  • upgraded flutter升级的颤振
  • upgraded pub files升级的 pub 文件
  • flutter clean扑干净
  • clean and build in Xcode在 Xcode 中清理和构建
  • added 'imageCache.clear()' in flutter code在颤振代码中添加了“imageCache.clear()”
  • invalidate caches and restart in android studio使缓存无效并在 android studio 中重新启动
  • flutter doctor -v : no issues found flutter doctor -v : 没有发现问题
  • checked the permissions (info.plist and added permission_handler package)检查权限(info.plist 并添加了permission_handler 包)
  • restarted the simulator重新启动模拟器
  • erased all contents and settings in simulator删除模拟器中的所有内容和设置
  • debugging with breakpoints weirdly stops the problem from happening a few times then after a few selections the app crashes again ¯\\ (ツ)使用断点调试奇怪地阻止问题发生几次然后在几次选择后应用程序再次崩溃¯\\ (ツ)

I'm sure its something straight forward but I feel like I have exhausted all my options and not sure where to go from here.我确信它是直接的,但我觉得我已经用尽了所有的选择,不知道从哪里开始。

Your code is right and it is a simulator problem,if you want to test it on IOS, you have to test on a real IPhone and here is a snippet on how properly you can do it:你的代码是正确的,这是一个模拟器问题,如果你想在 IOS 上测试它,你必须在真正的 iPhone 上进行测试,这里是一个关于你如何正确执行它的片段:

final imagePicker = ImagePicker();
    PickedFile pickedFile;
    if (fileType == FileType.Camera) {
      // Camera Part
      pickedFile = await imagePicker.getImage(
        source: ImageSource.camera,
        maxWidth: 480,
        maxHeight: 640,
        imageQuality: 25, // pick your desired quality
      );
      setState(() {
        if (pickedFile != null) {
          _storedFile = File(pickedFile.path);
        } else {
          print('No image selected.');
          return;
        }
      });
    } else if (fileType == FileType.Gallery) {
      // Gallery Part
      pickedFile = await imagePicker.getImage(
        source: ImageSource.gallery,
        maxWidth: 480,
        maxHeight: 640,
        imageQuality: 25,
      );
        } else {
          print('No image selected.');
          return;
        }
      });
    }

and don't forget the enum when the user picks the way he wants to get the image:并且当用户选择他想要获取图像的方式时不要忘记枚举:

enum FileType {
  Gallery,
  Camera,
  Video,
}

Edit: whenever you add a package that depends on native code, you should restart the whole app build b stopping your build and restarting it again so the native code compiles well, don't forget to restart before putting this package in your pubspec.yaml编辑:每当您添加依赖于本机代码的包时,您应该重新启动整个应用程序构建 b 停止您的构建并再次重新启动它以便本机代码编译良好,不要忘记在将此包放入您的 pubspec.yaml 之前重新启动

Hope it helps, if this answer helped you, don't forget to mark as answered and upvote it please.希望它有帮助,如果这个答案对您有帮助,请不要忘记标记为已回答并请点赞。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 有没有办法使用 Image_Picker 或来自 pub.dev 的类似插件从 Flutter 中的 ios 设备中选择 *.HEIC 文件? - Is there a way to pick *.HEIC files from an ios device in Flutter using Image_Picker or similar plugins from pub.dev? Flutter image_picker 视频库 OSError: Operation not allowed [Errno 1] IOS 13 - Flutter image_picker video gallery OSError: Operation not permitted [Errno 1] IOS 13 从图库中选择图像时,image_picker 使应用程序崩溃 IOS,控制台中没有任何日志 - While picking an image from gallery, image_picker is crashing the the app on IOS with no logs in the console 失去与设备的连接。 退出 (sigterm) - Flutter 崩溃 - Lost connection to device. Exited (sigterm) - Flutter Crash Flutter Image_Picker 在打开相机时崩溃 iOS 应用程序 - Flutter Image_Picker crash iOS app when open Camera 从图库中挑选图像时丢失与设备的连接错误 - lost connection to device error while image picking from gallery Flutter:在 iOS 模拟器 Firebase 中失去与设备的连接 - Flutter: Lost connection to device in iOS Simulator Firebase Flutter IOS 应用程序不会以 image_picker 启动:^0.6.3+4 - Flutter IOS app won't start with image_picker: ^0.6.3+4 image_picker:如果首先访问相机,则取消按钮在图库中不可见 - image_picker: Cancel button not visible in gallery, if camera was accessed first 在相机iOS上单击“使用照片”时失去与设备的连接 - Lost connection to device while clicking “Use Photo” on camera iOS
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM