Home > other >  Flutter - Just pick image and video using file picker
Flutter - Just pick image and video using file picker

Time:10-17

I'm writing an application that uses file_picker to select photos and videos. I use FileType.custom and declare the extension to only select photos and videos.

I want to get image to display with Image.file() and get video to display with NativeVideo(dart_vlc), so I used if-else clause to check by extension but processing time is quite long due to having to Check if the file has a video or image extension.

Does anyone have any ideas on this issue? Help me please. Sorry my english is not very good.

void pickFiles() async {
try {
  _paths = (await FilePicker.platform.pickFiles(
    type: FileType.custom,
    allowMultiple: false,
    onFileLoading: (FilePickerStatus status) => print(status),
    allowedExtensions: ['bmp', 'gif', 'jpeg', 'jpg', 'png', 'heic',
      'avi', 'flv', 'mkv', 'mov', 'mp4', 'mpeg', 'webm', 'wmv'
    ],
  ))?.files;
} on PlatformException catch (e) {
  e.toString();
} catch (e) {
  e.toString();
}
_fileName = _paths?.single.path;

if(_paths?.single.extension == 'bmp' || _paths?.single.extension == 'gif' || _paths?.single.extension == 'jpg' || _paths?.single.extension == 'png'
  || _paths?.single.extension == 'heic' || _paths?.single.extension == 'jpeg') {
  pathImagePicker.value = _fileName!;
} else {
  pathVideoPicker.value = _fileName!;
}

}

CodePudding user response:

You could try using the mime type of the file to determine if it is an image or video instead of the file extension.

  • Related