安装
image_picker: ^0.6.7+15
使用方法:
import 'package:image_picker/image_picker.dart'; final ImagePicker _picker = ImagePicker(); PickedFile pcikeFile; // 图片 pcikeFile = await _picker.getImage(source: ImageSource.gallery); pcikeFile = await _picker.getImage(source: ImageSource.camera); // 视频 pcikeFile = await _picker.getVideo(source: ImageSource.gallery, maxDuration: const Duration(seconds: 60)); pcikeFile = await _picker.getVideo(source: ImageSource.camera, maxDuration: const Duration(seconds: 60));
PickedFile 转 文件对象 file
import 'dart:io'; File file = File(pcikeFile.path)
在android 下,选择相册视频时有个bug, 得到的路径中文件名时xxx.jpg
如图,我选择一个相册中视频得到 pcikeFile.path 路径如下 ,
暂时解决办法:
先判断 android 环境 然后判断是否相册选择的视频,然后 修改 文件名字的后缀为 .mp4,就可以正常上传,正常使用了
var path = pcikeFile.path; String suffix = path.substring(path.lastIndexOf(".") + 1, path.length); if (Platform.isAndroid && imageSource == ImageSource.gallery && type == 'video') { suffix = 'mp4'; } int num = new DateTime.now().millisecondsSinceEpoch; String name = num.toString() + '.' + suffix; // 如下,这样把这个Map 按文件上传方式上传即可 Map item = {'path': path, 'name': name,};
亲测可用