sus*_*ngh 17
这对我来说是工作。
final file = File('pickedVid.mp4');
int sizeInBytes = file.lengthSync();
double sizeInMb = sizeInBytes / (1024 * 1024);
if (sizeInMb > 10){
// This file is Longer the
}
Run Code Online (Sandbox Code Playgroud)
Kam*_*esh 12
//定义函数
getFileSize(String filepath, int decimals) async {
var file = File(filepath);
int bytes = await file.length();
if (bytes <= 0) return "0 B";
const suffixes = ["B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"];
var i = (log(bytes) / log(1024)).floor();
return ((bytes / pow(1024, i)).toStringAsFixed(decimals)) + ' ' + suffixes[i];
}
Run Code Online (Sandbox Code Playgroud)
//如何调用函数
print(getFileSize('file-path-will-be-here', 1));
Run Code Online (Sandbox Code Playgroud)
//输出将是像这样的字符串:
97.5 KB
Run Code Online (Sandbox Code Playgroud)
这对我有用。我希望,这也能帮助你。非常感谢提问。
如果您具有文件的路径,则可以使用 dart:io
var file = File('the_path_to_the_video.mp4');
print(file.lengthSync());
Run Code Online (Sandbox Code Playgroud)
如果您使用的是xFile类型的最新 ImagePicker ,那么,
final XFile? photo = await _picker.pickImage(source: ImageSource.camera);
print(File(photo.path).lengthSync());
Run Code Online (Sandbox Code Playgroud)
将返回文件大小(以字节为单位)。
| 归档时间: |
|
| 查看次数: |
2917 次 |
| 最近记录: |