调用相机;
void _getImage() async {
try {
var image = await ImagePicker.pickImage(
source: ImageSource.gallery, maxWidth: 800);
print(image);
uploadImage(image.path, '0', onSuccess: (data) {
if (data != null) {
setState(() {
_info = data??'';
});
}
});
} catch (e) {
Toast.show("没有权限,无法打开相册", duration: 1000);
}
}
上传阿里云前端直传;
///上传图片
Future uploadImage(String imagePath, String vin,
{Function(String reslut) onSuccess}) async {
String name = DateTime.now().millisecondsSinceEpoch.toString() +
imagePath.substring(imagePath.lastIndexOf("."));
await DioUtil.instance
.requestEntityNetwork(Method.get, "${HttpApi.OSS_CAR_SIGNATURE}/$vin",
onSuccess: (data) async { //这个是去拿上传参数,包括地址签名等
FormData formData = FormData.from({
'key': data['dir'] + name,
'policy': data['policy'],
'OSSAccessKeyId': data['accessid'],
'success_action_status': 200,
'Signature': data['signature'],
'Filename': name,
'file': UploadFileInfo(File(imagePath), name)
});
Dio dio = Dio();
dio.options.responseType = ResponseType.plain;
try {
Response response = await dio.post(data['host'], data: formData);
String imageUrl = data['host'] + '/' + data['dir'] + name;
if (response.statusCode == 200) {
if (onSuccess != null) {
onSuccess(imageUrl);
}
}
} on DioError catch (e) {
Toast.show(e.message);
}
}, onError: (data) {
Toast.show("获取签名信息失败");
});
网友评论