【问题标题】:App crashing and imagepicker not working perfectly in flutter应用程序崩溃和图像选择器在颤动中无法正常工作
【发布时间】:2021-01-18 13:30:41
【问题描述】:

我正在尝试在颤动活动中添加图像。该图像是从我本地移动设备的图库中选择的。但是,一旦我选择图像,应用程序就会崩溃并显示以下错误:

java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=69, result=0, data=null} to activity {com.example.MainActivity}: java.lang.IllegalStateException: Reply already submitted

我正在使用 ImagePicker 和cropPicker,但似乎 imagepicker 无法正常工作。一旦我选择了图像,我希望图像显示在 CircleAvatar 中,但这并没有发生。 以下是我的代码:

class Signup extends StatefulWidget {
  @override
  _SignupState createState() => _SignupState();
}

class _SignupState extends State<Signup> {
    Widget build(BuildContext context) {
    return Scaffold(
      body: SafeArea(
        child: SizedBox(
          width: double.infinity,
          child: Padding(
            padding:
            EdgeInsets.symmetric(horizontal: getProportionateScreenWidth(20)),
            child: SingleChildScrollView(
              child: Column(
                children: [
                  SizedBox(height: SizeConfig.screenHeight * 0.04), // 4%
                  SizedBox(height: SizeConfig.screenHeight * 0.08),
                  Stack(
                      alignment: Alignment.bottomRight,
                      children: <Widget>[
                        CircleAvatar(
                          radius: 40,
                          child: _pickedImage == null ? Icon(Icons.person) : null,
                          backgroundImage: _pickedImage != null ? FileImage(_pickedImage) : null,
                        ),
                        Padding(
                          padding: EdgeInsets.all(0.0),
                          child: GestureDetector(
                              onTap: () {
                                _showPickOptionsDialog(context);
                              },
                              child: Icon(
                                Icons.camera_alt_outlined,
                              )
                          ),
                        ),
                      ]
                  ),
                ],
              ),
            ),
          ),
        ),
      ),
    );
  }

  _loadPicker(ImageSource source) async {
    File picked = await ImagePicker.pickImage(source: source);
    if (picked != null) {
      _cropImage(picked);
    }
    Navigator.pop(context);
  }

  _cropImage(File picked) async {
    File cropped = await ImageCropper.cropImage(
      androidUiSettings: AndroidUiSettings(
        statusBarColor: Colors.red,
        toolbarColor: Colors.red,
        toolbarTitle: "Crop Image",
        toolbarWidgetColor: Colors.white,
      ),
      sourcePath: picked.path,
      aspectRatioPresets: [
        CropAspectRatioPreset.original,
        CropAspectRatioPreset.ratio16x9,
        CropAspectRatioPreset.ratio4x3,
      ],
      maxWidth: 800,
    );
    if (cropped != null) {
      setState(() {
        _pickedImage = cropped;
      });
    }
  }


  void _showPickOptionsDialog(BuildContext context) {
    showDialog(context: context,
        builder: (context) => AlertDialog(
          content: Column(
            mainAxisSize: MainAxisSize.min,
            children: [
              ListTile(
                title: Text('Pick from Gallery'),
                onTap: (){
                  _loadPicker(ImageSource.gallery);
                },
              ),
              ListTile(
                title: Text('Take a Picture'),
                onTap: (){
                  _loadPicker(ImageSource.camera);
                },
              ),
            ],
          ),
        )
    );
  }
}

有人可以帮我解决这个问题吗?

【问题讨论】:

    标签: flutter dart


    【解决方案1】:

    您需要将以下代码添加到您的android manifest 文件中

      <activity
       android:name="com.yalantis.ucrop.UCropActivity"
       android:screenOrientation="portrait"
       android:theme="@style/Theme.AppCompat.Light.NoActionBar"/>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-07-05
      • 1970-01-01
      • 2020-05-29
      • 2021-10-18
      • 1970-01-01
      • 1970-01-01
      • 2020-03-16
      相关资源
      最近更新 更多