flutter 圖片視頻選擇組件 image_picker


安裝 

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,};

親測可用

 

 

 

 

 

 

 

 

 

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM