How to Select Video from Camera and Gallery in Flutter?
Last Updated :
24 Apr, 2025
We can add video from the gallery as well as from the camera using the image_picker package in Flutter. For this, you’ll need to use your real device or web version. Follow the below steps to display the images from the gallery:
Step By Step Implementation
Step 1: Create a New Project in Android Studio
To set up Flutter Development on Android Studio please refer to Android Studio Setup for Flutter Development, and then create a new project in Android Studio please refer to Creating a Simple Application in Flutter.
Step 2: Add the dependency to your pubspec.yaml file
Dart
dependencies:
flutter:
sdk: flutter
cupertino_icons: ^1.0.2
image_picker: ^0.8.6+1
Step 3: Create variable for image picker and file
Dart
File? videoFile;
final picker = ImagePicker();
Step 4: Use the below code in the main.dart file
Dart
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:image_picker/image_picker.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData(primaryColor: Colors.green),
home: const VideoSelector(),
debugShowCheckedModeBanner: false,
);
}
}
class VideoSelector extends StatefulWidget {
const VideoSelector({super.key});
@override
State<VideoSelector> createState() => _VideoSelectorState();
}
class _VideoSelectorState extends State<VideoSelector> {
File? galleryFile;
final picker = ImagePicker();
@override
Widget build(BuildContext context) {
// display image selected from gallery
return Scaffold(
appBar: AppBar(
title: const Text('Gallery and Camera Access'),
backgroundColor: Colors.green,
actions: const [],
),
body: Builder(
builder: (BuildContext context) {
return Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
ElevatedButton(
style: ButtonStyle(
backgroundColor: MaterialStateProperty.all(Colors.green)),
child: const Text('Select video from Gallery and Camera'),
onPressed: () {
_showPicker(context: context);
},
),
const SizedBox(
height: 20,
),
SizedBox(
height: 200.0,
width: 300.0,
child: galleryFile == null
? const Center(child: Text('Sorry nothing selected!!'))
: Center(child: Text(galleryFile!.path)),
),
const Padding(
padding: EdgeInsets.symmetric(vertical: 18.0),
child: Text(
"GFG",
textScaleFactor: 3,
style: TextStyle(color: Colors.green),
),
)
],
),
);
},
),
);
}
void _showPicker({
required BuildContext context,
}) {
showModalBottomSheet(
context: context,
builder: (BuildContext context) {
return SafeArea(
child: Wrap(
children: <Widget>[
ListTile(
leading: const Icon(Icons.photo_library),
title: const Text('Gallery'),
onTap: () {
getVideo(ImageSource.gallery);
Navigator.of(context).pop();
},
),
ListTile(
leading: const Icon(Icons.photo_camera),
title: const Text('Camera'),
onTap: () {
getVideo(ImageSource.camera);
Navigator.of(context).pop();
},
),
],
),
);
},
);
}
Future getVideo(
ImageSource img,
) async {
final pickedFile = await picker.pickVideo(
source: img,
preferredCameraDevice: CameraDevice.front,
maxDuration: const Duration(minutes: 10));
XFile? xfilePick = pickedFile;
setState(
() {
if (xfilePick != null) {
galleryFile = File(pickedFile!.path);
} else {
ScaffoldMessenger.of(context).showSnackBar(// is this context <<<
const SnackBar(content: Text('Nothing is selected')));
}
},
);
}
}
Different Properties in pickVideo function
1. Source of video
It will take an enum as its value
- Value: [ImageSource.camera, ImageSource.gallery]
- Example: source: ImageSource.gallery
Note: Camera Source will not work in the web version.
2. Which Camera device you want to choose front or rear?
It will also take enum like
- Value: [CameraDevice.front, CameraDevice.rear]
- Example: preferredCameraDevice: CameraDevice.front
3. Duration of max video selected
It will take duration as a value
Example: maxDuration: const Duration(minutes: 10));
For Android:
Add this permission to android/app/src/main/AndroidManifest.xml
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.INTERNET"/>
For iOS:
Add the following keys to your Info.plist file, located in <project root>/ios/Runner/Info.plist:
NSPhotoLibraryUsageDescription
NSCameraUsageDescription
NSMicrophoneUsageDescription
Output:
When no video is selected, it will result:
When the button is pressed, it will ask for accessing photos, media, and files on your device as shown below:
When permission is given to access media and any videos are selected from the gallery or camera, its path will be displayed on the screen as shown below:
If you select the camera:
If you select the gallery:
Explanation:
- Import image_picker package in main.dart file.
- For selecting the source of video like gallery, camera use _showPicker function.
- After selecting, we have the async function getVideo() and that will open the gallery and camera as per your selection in _showPicker function.
- The Video path will be displayed after loaded.
Similar Reads
How to Move Camera to any Position in Google Maps in Flutter?
Google Maps is of the most used application nowadays for navigation. If search for any location our camera on Google Map moves to that position. In this article, we are going to see how to Move the Camera to any Position in Google Maps in Flutter. Step By Step ImplementationStep 1: Create a New Pro
4 min read
Gallery Access and Camera in Flutter
We can add images from the gallery using the image_picker package in Flutter. For this, you'll need to use your real device.Steps to Implement Gallery and Camera AccessStep 1: Create a new flutter applicationCreate a new Flutter application using the command Prompt. To create a new app, write the be
3 min read
Flutter - Navigate From One Screen to Another
Flutter apps may have multiple screens or pages. Pages are groups of functionalities. The user navigates between different pages to use different functionalities. Concepts like pages are called routes in Flutter. We can use Navigator.push() to navigate to a new route and Navigator.pop() to navigate
3 min read
Flutter - Select Single and Multiple Files From Device
We will explore how to select single, or multiple files from our device with different conditions. Sometimes it happens that we want to select only a single type of file or with a fixed extension. If you are looking for the same just read the article till the end. Step By Step Implementation 1. Crea
6 min read
How to Import Data From One Page to Another in Flutter?
Flutter is Googleâs Mobile SDK to build native iOS and Android, Desktop (Windows, Linux, macOS), Web apps from a single codebase. When building applications with Flutter everything towards Widgets â the blocks with which the flutter apps are built. They are structural elements that ship with a bunch
5 min read
How to Build a Video Calling App in Flutter?
Video Calling has become a necessary feature in most real-time communication applications used around the world. Its primary application is to connect people from all around the world along with other obvious applications. Chat applications like Facebook, WhatsApp, and Instagram all have video calli
4 min read
Flutter - Extract Data From an Image to Text
Google ML kit provides many features, one of them is Image to Text Extraction, through which we can simply extract text from an image. To extract text from the image first we need to take an image as input either from the gallery or camera for which we will use 'image_picker: ^1.0.4' (dependency fro
3 min read
Flutter - Get District and State Name From Pincode
This Flutter application allows users to enter a PIN code and fetch corresponding details such as district, state, and country using the "http://www.postalpincode.in" API. Fetching data from an external API is a common scenario in mobile app development. We've employed the http package in Flutter to
7 min read
Flutter - How to Get Instagram Username Data
Flutter is a cross-platform application development toolkit developed and maintained by Google. Flutter redefines cross-platform app development by combining great design with superb capabilities. Flutter was released to the public in 2017, and since then many large corporations have shifted towards
3 min read
How to Get Coordinates Widgets using rect_getter Package in Flutter?
Sometimes we need to get a widget's location or show some overlay widget or something similar over a widget whose location on the screen could be dynamic. In this article, we are going to discuss the way to get coordinates of a widget using the rect_getter package. Follow the steps below : 1. Create
9 min read