Flutter Saver

A Flutter package for saving images and files to the download folders across different platforms (Android, iOS, Windows, macOS, and Web).

Overview

Flutter Saver provides simple methods to save images and download files from network URLs to the download directory or other specific directories on various platforms. It handles platform-specific directory paths, file naming, and error handling.

Installation

Add this to your package's pubspec.yaml file:

dependencies:
  flutter_saver: ^0.0.5

Then run:

flutter pub get add flutter_saver

Usage

Import the package:

import 'package:flutter_saver/flutter_saver.dart';

Save Image Files

Windows/Web

bool success = await FlutterSaver.saveImageWindowsWeb(
  fileImage: imageFile,
  fileName: 'my_image', // Optional (random name will be generated if not provided)
  type: 'jpg', // Default is 'jpg'
);

Android

bool success = await FlutterSaver.saveImageAndroid(
  fileImage: imageFile,
  fileName: 'my_image', // Optional
  type: 'png', // Default is 'jpg'
  pathDirectory: AndroidDirectory.pictures, // Default is AndroidDirectory.downloads
);

iOS

bool success = await FlutterSaver.saveImageIos(
  fileImage: imageFile,
  fileName: 'my_image', // Optional
  type: 'png', // Default is 'jpg'
  pathDirectory: DirectoryType.pictures, // Default is DirectoryType.downloads
);

macOS

bool success = await FlutterSaver.saveImageMacOs(
  fileImage: imageFile,
  fileName: 'my_image', // Optional
  type: 'png', // Default is 'jpg'
  pathDirectory: MacDirectoryType.pictures, // Default is MacDirectoryType.downloads
);

Save Network Files

Windows/Web

bool success = await FlutterSaver.saveNetworkFileWindowsWeb(
  link: 'https://example.com/image.jpg',
);

Android

bool success = await FlutterSaver.saveNetworkFileAndroid(
  link: 'https://example.com/image.jpg',
  pathDirectory: AndroidDirectory.pictures, // Default is AndroidDirectory.downloads
);

iOS

// Create an instance
final flutterSaver = FlutterSaver();

bool success = await flutterSaver.saveNetworkFileIos(
  link: 'https://example.com/image.jpg',
  directoryType: DirectoryType.pictures, // Default is DirectoryType.downloads
);

macOS

// Create an instance
final flutterSaver = FlutterSaver();

bool success = await flutterSaver.saveNetworkFileMac(
  link: 'https://example.com/image.jpg',
  pathDirectory: MacDirectoryType.pictures, // Default is MacDirectoryType.downloads
);

Directory Types

Android Directory Types

Flutter Saver supports the following directory types on Android:

  • AndroidDirectory.downloads - Device's download directory
  • AndroidDirectory.pictures - Pictures directory
  • AndroidDirectory.movies - Movies directory
  • AndroidDirectory.dcim - DCIM directory
  • AndroidDirectory.documents - Documents directory
  • AndroidDirectory.music - Music directory

iOS Directory Types

For iOS, the following directory types are supported:

  • DirectoryType.downloads - Downloads directory
  • DirectoryType.music - Music directory
  • DirectoryType.podcasts - Podcasts directory
  • DirectoryType.ringtones - Ringtones directory
  • DirectoryType.alarms - Alarms directory
  • DirectoryType.notifications - Notifications directory
  • DirectoryType.pictures - Pictures directory
  • DirectoryType.movies - Movies directory
  • DirectoryType.dcim - DCIM directory
  • DirectoryType.documents - Documents directory
  • DirectoryType.screenshots - Screenshots directory
  • DirectoryType.audiobooks - Audiobooks directory

macOS Directory Types

For macOS, the following directory types are available:

  • MacDirectoryType.downloads - Downloads directory
  • MacDirectoryType.pictures - Pictures directory
  • MacDirectoryType.movies - Movies directory

Error Handling

All methods return a Future<bool> that resolves to:

  • true if the file was successfully saved
  • false if there was an error during the save operation

Errors are logged to the console when in debug mode.

Features

  • Save image files to platform-specific directories
  • Download files from URLs to platform-specific directories
  • Support for custom file names
  • Support for custom file extensions
  • Support for custom directory paths
  • Random file name generation if no name is provided
  • Automatic file name handling to prevent overwriting existing files
  • Content-type detection for proper file extension assignment

How to use


import 'package:flutter_saver/flutter_saver.dart';

  • Steps:

1-Go to the following path in your project:

android/app/src/main/AndroidManifest.xml

2- Make sure you add the following permissions to the AndroidManifest.xml file:

  • Camera and internet access.

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.CAMERA" />

  • Add this line code to application AndroidManifest.xml
android:requestLegacyExternalStorage="true"
<application
        android:label="avaterbetter"
        android:name="${applicationName}"
        android:icon="@mipmap/ic_launcher"
        android:requestLegacyExternalStorage="true">

iOS

     <key>NSPhotoLibraryUsageDescription</key>
    <string>We need access to your photo library to select images for editing.</string>
    <key>NSCameraUsageDescription</key>
    <string>We need access to your camera to take photos for editing.</string>
    <key>NSPhotoLibraryUsageDescription</key>
    <string>We need access to your photos to save images.</string>
    <key>NSDocumentsFolderUsageDescription</key>
    <string>We need access to your documents folder to save files.</string>

macOS installation

Since the macOS implementation uses file_selector, you will need to add a filesystem access entitlement:

  <key>com.apple.security.files.user-selected.read-only</key>
  <true/>

Dependencies

  • external_path_ios_mac: For iOS and macOS directory access

  • native_android_path: For Android directory access

  • path_provider: For cross-platform directory access

  • http: For network file downloads

  • path: For file path manipulation

  • suport this format

{
  'image/jpeg': '.jpg',
  'image/png': '.png',
  'video/mp4': '.mp4',
  'application/pdf': '.pdf',
  'application/zip': '.zip',
  'image/gif': '.gif',
  'image/webp': '.webp',
  'image/svg+xml': '.svg',
  'image/tiff': '.tiff',
  'image/vnd.microsoft.icon': '.ico',
  'image/vnd.djvu': '.djvu',
  'image/vnd.adobe.photoshop': '.psd',
  'image/x-ms-bmp': '.bmp',
  'image/x-icon': '.ico',
  'image/x-ico': '.ico',
  'image/x-xbitmap': '.xbm',
  'image/x-png': '.png',
  'application/x-msdownload': '.exe',
  'application/vnd.openxmlformats-officedocument.presentationml.presentation':
      '.pptx',
  'application/vnd.openxmlformats-officedocument.wordprocessingml.document':
      '.docx',
  'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet': '.xlsx',
  '.dwg': '.dwg',

  // Video formats
  'video/x-msvideo': '.avi',
  'video/mpeg': '.mpeg',
  'video/quicktime': '.mov',
  'video/x-ms-wmv': '.wmv',
  'video/x-flv': '.flv',
  'video/webm': '.webm',
  'video/ogg': '.ogv',
  'video/x-matroska': '.mkv',
  'video/3gpp': '.3gp',
  'video/3gpp2': '.3g2',

  // Audio formats
  'audio/mpeg': '.mp3',
  'audio/wav': '.wav',
  'audio/x-wav': '.wav',
  'audio/ogg': '.ogg',
  'audio/flac': '.flac',
  'audio/aac': '.aac',
  'audio/mp4': '.m4a',
  'audio/opus': '.opus',
  'audio/x-ms-wma': '.wma',
  'audio/x-aiff': '.aiff',
  'audio/x-matroska': '.mka',
  // more ...
};

Additional information

If you have any issues, questions, or suggestions related to this package, please feel free to contact us at swan.dev1993@gmail.com. We welcome your feedback and will do our best to address any problems or provide assistance. For more information about this package, you can also visit our GitHub repository where you can find additional resources, contribute to the package's development, and file issues or bug reports. We appreciate your contributions and feedback, and we aim to make this package as useful as possible for our users. Thank you for using our package, and we look forward to hearing from you!

License

This package is released under the MIT License.

Libraries

flutter_saver