我使用相机包打开相机,并采取视频和图像的一个屏幕在过去我使用image_picker,但它满足我的要求,使用相机和画廊的视频和图像,所以现在我想正确的方式来打开画廊的视频和图像我怎么能用相机 Package 或其他方式做呢?
ikfrs5lh1#
使用file_picker(https://pub.dev/packages/file_picker)包,它允许选择任何扩展名的文件。样本代码:-
file_picker
FilePickerResult? result = await FilePicker.platform.pickFiles( type: FileType.custom, allowedExtensions: ['jpg', 'pdf', 'doc'], );
完整代码:-
import 'package:flutter/material.dart'; import 'package:file_picker/file_picker.dart'; void main() => runApp(const Example()); class Example extends StatefulWidget { const Example({super.key}); @override State<Example> createState() => _ExampleState(); } class _ExampleState extends State<Example> { openExplorer() async { FilePickerResult? result = await FilePicker.platform.pickFiles( type: FileType.custom, allowedExtensions: ['jpg', 'pdf', 'doc'], ); if (result != null) { print("File selected"); } } @override Widget build(BuildContext context) { return MaterialApp( home: Scaffold( body: GestureDetector( onTap: (() { openExplorer(); }), child: const Center(child: Text("Open Gallery")))), ); } }
输出:-
1条答案
按热度按时间ikfrs5lh1#
使用
file_picker
(https://pub.dev/packages/file_picker)包,它允许选择任何扩展名的文件。样本代码:-
完整代码:-
输出:-