android - 来自相机的图像选择器未发送到 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();
}
}
解决方案
推荐阅读
- python - Python Bokeh 无法导入
- python - 无法正确配置 PATH 和 PYTHONPATH
- html - 隐藏基于设备/操作系统而不是屏幕大小
- environment-variables - 如何将性能与 ld_preload 一起使用?
- node.js - npm build - 不工作,“未找到 npm-build.html”
- python - 如何在 mne.io.read_raw_fif() 中使用 CSV?
- google-apps-script - 例外:访问带有 id google sheet 的文档时服务电子表格失败
- mongodb - 仅在聚合查询中显示某些嵌套字段
- python - Python随机数程序
- html - SCSS:嵌套链接:悬停属性不起作用?