标签:lan print github yaml path pap turn gallery img
Github地址: https://github.com/flutter/plugins/tree/master/packages/image_picker
packages地址: https://pub.dev/packages/image_picker
1、将其添加到包的pubspec.yaml文件中:
image_picker: ^0.4.5
2、安装
flutter pub get
3、导入
import ‘package:image_picker/image_picker.dart‘;
4、上传图片的两种方式
使用相机
var image = await ImagePicker.pickImage(source: ImageSource.camera);
使用图库
var image = await ImagePicker.pickImage(source: ImageSource.gallery);
5、上传图片
_openGallery() async { var image = await ImagePicker.pickImage(source: ImageSource.gallery); ShopPaperImgDao.uploadImg(image).then((res){ if(res[‘code‘] == 200){ // 上传成功 }else{} }).catchError((e){ print(e.toString()); }); }
import ‘dart:async‘; import ‘dart:io‘; import ‘package:dio/dio.dart‘; class ShopPaperImgDao { // 上传图片 static Future uploadImg(imgfile) async{ String path = imgfile.path; var name = path.substring(path.lastIndexOf("/") + 1, path.length); FormData formData = new FormData.from({ "file": new UploadFileInfo(new File(path), name) }); Response response; Dio dio =new Dio(); response =await dio.post(‘后端接口‘,data: formData); if(response.statusCode == 200){ return response.data; }else{ throw Exception(‘后端接口异常‘); } } }
6、结语
注意:
flutter pub get 之后可能需要重新 flutter run
标签:lan print github yaml path pap turn gallery img
原文地址:https://www.cnblogs.com/wyhlightstar/p/11082675.html