安裝
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,};
親測可用