首页 > 解决方案 > 来自相机的图像选择器未发送到 API

问题描述

我在应用程序中使用图像选择器,我从相机中选择图像并将它们发送到 API,但我不知道它从后端收到错误,因为无法保存,但是当我从图库中选择它时它可以工作如果我使用相机并将其提供给图像裁剪器功能也很好,它也可以在这种情况下使用,只是不能使用唯一的相机。这是我的代码:

  Future open_camera() async {
  var _pickedFile = await ImagePicker().getImage(source: ImageSource.camera);
  print("-----path-------" + _pickedFile.path.toString());
  if (_pickedFile != null && _pickedFile.path != null) {
  
  setState(() {
    Cfile=File(_pickedFile.path);
    });
   }
  }

这是我调用 API

Future<void>AddDamamge(String vID) async {
try
{
  _showProgress(context);
  Uri uri = Uri.parse(WebServices.Save_Damages+SessionManager.getString(Constant.Token)+

      "&vehicle_id="+vID);
  print(uri);
  var _request = https.MultipartRequest('POST', uri);

  if (Cfile != null) {
    
    _request.files.add(await https.MultipartFile.fromPath(
        "VehicleDamage[0][image_file]", Cfile.path));
  }
  _request.fields["VehicleDamage[0][damage_comment]"] = _controller.text;

  print("------request11------" + _request.fields.toString() );
  print("------request--22----" + _request.files.toString() );
  print("------request----33--" + _request.toString() );

  _hideProgress();
  var streamedResponse = await _request.send();
  print("showing response"+streamedResponse.statusCode.toString());
  https.Response res = await https.Response.fromStream(streamedResponse);
  print('response.body ' + res.statusCode.toString());
  print("-----respnse----" + res.body.toString());
  _hideProgress();

  if (res.statusCode == 200) {
    var bodydata = json.decode(res.body);
    _hideProgress();
    if (bodydata["success"]) {

      UtilMethod.SnackBarMessage(_scaffoldKey,
          "Damage Added Successfully !!!");
    //          Navigator.pushReplacementNamed(
    //              context, Constant.ChatUserActivityList);
      Cfile=null;

      Navigator.pop(context, Constant.VendorConfirmPickup);
    }
    else if(bodydata["success"]==false){
      UtilMethod.SnackBarMessage(_scaffoldKey,
          bodydata["data"]["message"].toString());
      _hideProgress();
    }
    else {
      if (bodydata["is_expire"] == Constant.Token_Expire) {
        UtilMethod.showSimpleAlert(
            context: context,
            heading: "Alert",
            message:
            "Token Expire");
      }
      else{
        UtilMethod.SnackBarMessage(_scaffoldKey, "Some Error Occured");
      }
    }
  }
}
on Exception catch (e) {
  print(e.toString());
  _hideProgress();
}
}

标签: androidiosflutterdart

解决方案


推荐阅读