【问题标题】:White/blank screen after close camera in FlutterFlutter中关闭相机后的白屏/黑屏
【发布时间】:2020-04-23 05:32:25
【问题描述】:

我有一个小部件可以在我的应用程序上拍照:

final File imageFile =
        await ImagePicker.pickImage(source: ImageSource.camera).then((test) {
      print('TEST $test');
      return test;
    });

我可以毫无错误地打开相机并拍照,但是当我尝试返回或接受我拍摄的照片时,应用程序会显示白屏,并且控制台根本没有显示任何错误。

这在真实设备(小米红米 Note 8t)上失败,但在 Android 模拟器上有效。

当我拿起相机时,我能看到的唯一消息是Lost connection to device.

【问题讨论】:

    标签: flutter flutter-dependencies


    【解决方案1】:

    我找到了flutter 2的解决方案

    在安卓中

    您需要将此添加到您的 3 个清单(调试、主要、配置文件)

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

    然后用 try catch 选择图像,像这样

    Future getbehind() async {
        try{
          final pickedFile = await picker.getImage(source: (ImageSource.camera:ImageSource.gallery),)
              .then((value) {
            setState(() {
              if (value != null) {
                behind = File(value.path);
              } else {
                print('No image selected.');
              }
            });
          });
    
    
        }catch(e){
    
        }
    

    在发布模式下运行应用程序并完成!

    【讨论】:

      【解决方案2】:

      修复了添加 try catch:

      Future<Null> _pickImageFromCamera(BuildContext context, int index) async {
          File imageFile;
          try {
            imageFile = await ImagePicker.pickImage(source: ImageSource.camera)
            .then((picture) {
              return picture; // I found this .then necessary
            });
          } catch (eror) {
            print('error taking picture ${error.toString()}');
          }
          setState(() => this._imageFile = imageFile);
        }
      

      【讨论】:

      • 我有同样的情况,你的代码对我不起作用
      • 这可能与其他问题有关。这绝对解决了我的问题。您是否正在尝试包括 Flutter 2 在内的最新版本?
      • 是的,我使用的是最新版本,包括 Flutter 2.0.3 版本,这在真实设备上也失败了(小米红米 Note 7)。我能看到的唯一消息是与设备的连接丢失。当我拿起相机。画廊工作正常
      • 它可以完全不同。尝试使用您的代码发布一个新问题
      猜你喜欢
      • 1970-01-01
      • 2020-04-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多