1、从手机相机选择器获取图片
resultList =await MultiImagePicker.pickImages(
// 选择图片的最大数量
maxImages:_medicalImageMaxLength -_medicalImages.length,
// 是否支持拍照
enableCamera:true,
materialOptions:MaterialOptions(
// 显示所有照片,值为 false 时显示相册
startInAllView:true,
allViewTitle:'所有照片',
actionBarColor:'#2196F3',
textOnNothingSelected:'没有选择照片'
),
);
2、提交到服务器
ByteData data =await image.getByteData();
final buffer = data.buffer;
FormData form =FormData.fromMap({
'name': image.name,
'file':MultipartFile.fromBytes(buffer.asUint8List(data.offsetInBytes, data.lengthInBytes)),
});
await Dio().post(r.host, data: form);
3、重点:ByteData转File
final filename = 'test.jpg';
var bytes = await rootBundle.load("assets/test.jpg");
String dir = (await getTemporaryDirectory()).path;
File file = writeToFile(bytes,'$dir/$filename');
Future<void> writeToFile(ByteData data, String path) {
final buffer = data.buffer;
return new File(path).writeAsBytes(
buffer.asUint8List(data.offsetInBytes, data.lengthInBytes));
}