Search code examples
flutterimagevideocamera

Flutter: How to open gallery for both image and video


I use camera package to open camera and take both video and image by one screen

in the past I used image_picker but it's satisfy my requirments for use camera and gallery to both video and image so now I want the right way to open the gallery with both videos and images

how i can do it with camera package or otherwise?


Solution

  • Use file_picker (https://pub.dev/packages/file_picker) package, which allows to select file with any extension.

    Sample Code : -

    FilePickerResult? result = await FilePicker.platform.pickFiles(
      type: FileType.custom,
      allowedExtensions: ['jpg', 'pdf', 'doc'],
    );
    

    Complete Code : -

    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")))),
        );
      }
    }
    

    Output : -

    enter image description here