我想在使用文件选择器检索文件之前压缩文件大小。然后我尝试压缩文件,这就是发生的事情
文件选择器
压缩 1
使用 flutter_image_compress
Future<File> compressFile() async {
try{
//PlatformFile file = _paths.first;
File file = File(_fileName);
final filePath = file.absolute.path;
// Create output file path
// eg:- "Volume/VM/abcd_out.jpeg"
final lastIndex = filePath.lastIndexOf(new RegExp(r'.jp'));
final splitted = filePath.substring(0, (lastIndex));
final outPath = "${splitted}_out${filePath.substring(lastIndex)}";
var result = await FlutterImageCompress.compressAndGetFile(
file.absolute.path, outPath,
quality: 5,
);
print(file.lengthSync());
print(result.lengthSync());
return result;
}
catch(e){
var snackBar = SnackBar(content: Text('$e'),
backgroundColor: Colors.red,);
_scaffoldKey.currentState.showSnackBar(snackBar);
print('$e');
}
}
结果
压缩 2
compressingPhoto() async {
try{
final tDirectory = await getTemporaryDirectory();
final path = tDirectory.path;
//final path = tDirectory.path;
// PlatformFile _paths;
// File file = File(_paths.path);
File file = File(_fileName);
ImD.Image mImageFile = ImD.decodeImage(file.readAsBytesSync());
final compressedImageFile =
File('$path/img_$_fileName.jpg')..writeAsBytesSync(ImD.encodeJpg(mImageFile,
quality: 80));
setState(() {
file = compressedImageFile;
print(file.lengthSync);
});
}
catch(e){
var snackBar = SnackBar(content: Text('$e'),
backgroundColor: Colors.red,);
_scaffoldKey.currentState.showSnackBar(snackBar);
print('$e');
}