lecle_native_camera

A Flutter project to open device's camera to capture image or video (or you can pick image on Android platform) and get the file's path and the file's type on Android and iOS platforms.

Android

Paste the following attribute in the manifest tag in the AndroidManifest.xml


xmlns:tools="http://schemas.android.com/tools"

For example:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:tools="http://schemas.android.com/tools"
          package="your package...">

Add these permissions to your AndroidManifest.xml

    <uses-feature android:name="android.hardware.camera.any"
        android:required="true" />
    <uses-feature android:name="android.hardware.camera.autofocus"
        android:required="false" />
    
    <uses-permission android:name="android.permission.CAMERA"  
        android:required="true"
        android:requiredFeature="true" />

    <uses-permission android:name="android.permission.FLASHLIGHT" />
    <uses-feature android:name="android.hardware.camera.flash" android:required="false"
        tools:targetApi="eclair" />

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

Create xml folder in android/app/src/main/res (for example: file_paths.xml) and add the lines below to the created xml file

<?xml version="1.0" encoding="utf-8"?>
<paths>
    <external-path
        name="external_files"
        path="." />
</paths>

After create your own file provider and define your own path paste them into this and add to your AndroidManifest.xml


<provider android:name="androidx.core.content.FileProvider"
          android:authorities="[your_custom_fileProvider_path]"
          android:exported="false" android:grantUriPermissions="true">
    <meta-data android:name="android.support.FILE_PROVIDER_PATHS" 
               android:resource="@xml/[your_custom_fileProvider_file_name]" />
</provider>

iOS

Add these lines to your Info.plist file


<key>NSCameraUsageDescription</key>
<string>Our app needs permission to access the camera</string>

<key>NSMicrophoneUsageDescription</key>
<string>$(PRODUCT_NAME) needs permission to access the microphone to record the audio along with the video</string>

<key>NSPhotoLibraryAddUsageDescription</key>
<string>$(PRODUCT_NAME) needs permission to save photo to your gallery</string>

<key>NSPhotoLibraryUsageDescription</key>
<string>$(PRODUCT_NAME) needs permission to access your photos</string>

<key>NSBonjourServices</key>
<array>
    <string>_dartobservatory._tcp</string>
</array>

<key>NSAppTransportSecurity</key>
<dict>
  <key>NSAllowsArbitraryLoads</key>
  <true/>
</dict>

How to use

  • Simply call openNativeCamera method from LecleNativeCamera class to open the native camera to capture video or image (or you can pick image on Android platform) and the method will return the path of the captured file and the type of the file.
  • The return data will be converted into a FileData object and you can use this object to get the file's path and the file's type easily.

Properties

The properties in the openNativeCamera method and platform that the platform they work on

Properties Android iOS
fileProviderPath X
imageExtension X X
videoExtension X X
fileName X X
androidOptionSheetTitle X
canPickFile X
videoQuality X X
imageQuality X X
imageWidth X
imageHeight X
customSaveFolder X
saveFolder X

Example


ElevatedButton(
    onPressed: () {
      LecleNativeCamera.openNativeCamera(
        fileProviderPath: 'your_custom_fileProvider_path', // This must be the same with the custom path that you defined in the AndroidManifest file
        imageExtension: ImageExtension.jpeg,
        videoQuality: VideoQuality.typeHigh,
        videoExtension: VideoExtension.mov,
        imageQuality: 100,
        imageWidth: 2160,
        imageHeight: 3804,
        androidOptionSheetTitle: 'your_custom_title',
        canPickFile: true,
        fileName: 'your_custom_file_name',
        customSaveFolder: 'Test Folder',
      ).then(
        (value) async {
          if (value != null) {
            print('$value');
    
            if (value.fileType == FileType.image &&
                value.filePath != null) {
              setState(() {
                imageFile = File(value.filePath!);
                imageFile!.length().then((value) {
                  print('File size ::: $value');
                });
                videoFile = null;
                _controller = null;
              });
            } else if (value.fileType == FileType.video &&
                value.filePath != null) {
              setState(() {
                videoFile = File(value.filePath!);
                videoFile!.length().then((value) {
                  print('File size ::: $value');
                });
                imageFile = null;
                _controller = VideoPlayerController.file(videoFile!);
                _controller!.initialize().then(
                      (value) {
                    setState(() {});
                    _controller!.addListener(() {
                      if (_controller?.value.position ==
                          _controller?.value.duration) {
                        setState(() {
                          _isPlaying = false;
                        });
                      }
                    });
                  },
                );
              });
            }
          }
        },
      );
    },
    child: const Text('Open native camera'),
),