Flutter:如何同时打开图像和视频库

myzjeezk  于 2023-01-27  发布在  Flutter
关注(0)|答案(1)|浏览(92)

我使用相机包打开相机,并采取视频和图像的一个屏幕
在过去我使用image_picker,但它满足我的要求,使用相机和画廊的视频和图像,所以现在我想正确的方式来打开画廊的视频和图像
我怎么能用相机 Package 或其他方式做呢?

ikfrs5lh

ikfrs5lh1#

使用file_pickerhttps://pub.dev/packages/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")))),
    );
  }
}

输出:-

相关问题