XFile 中图像和视频的区别

Difference between image and video in XFile

这是我的拍照方法:

handleTakePhoto() async {
 Navigator.pop(context);
 XFile? file = await ImagePicker()
    .pickImage(source: ImageSource.camera, maxWidth: 960, maxHeight: 675);
 setState(() {
   this.file = file;
   bytes = File(file!.path).readAsBytesSync();
 });
}

虽然这是用来拍视频的:

handleTakeVideo() async {
 Navigator.pop(context);
 XFile? file = await ImagePicker()
     .pickVideo(source: ImageSource.camera, maxDuration: const Duration(seconds: 10));
 setState(() {
   this.file = file;
   bytes = File(file!.path).readAsBytesSync();
 });
}

如您所见,它们实际上是相同的,但稍后在我的程序中我需要知道文件是图像还是视频。我该怎么办?

它们的拍照和录像方法可能相同,但图像文件和视频文件不相同,包含不同的文件扩展名。图像文件可以是 .jpg、.jpeg 或 .png。您可以打印文件路径以自己查看这些文件。 print(file.path)。 查看此 以帮助区分它们。