easy_file_saver 1.1.0 copy "easy_file_saver: ^1.1.0" to clipboard
easy_file_saver: ^1.1.0 copied to clipboard

PlatformAndroid

Flutter plugin to easily save file to various storage directories.

easy_file_saver #

Flutter plugin to easily save file to various storage directories.

Features #

  • Save Uint8List bytes to a file.
  • Supports multiple storage directories

    Android:

    • EasyFileSaveDirectory.cache: Temporary cache directory.
    • EasyFileSaveDirectory.internal: App-specific internal storage (private to the app).
    • EasyFileSaveDirectory.external: Public media storage (scoped to app).
    • EasyFileSaveDirectory.download: Public downloads folder.
    • EasyFileSaveDirectory.saf: User-selected directory via Storage Access Framework (SAF).

    IOS:

    • Not supported yet.

Installation #

Add easy_file_saver to your pubspec.yaml file:

flutter pub add easy_file_saver

Configuration #

Android #

For Android versions 10 (SDK 29) and below, you need to add the WRITE_EXTERNAL_STORAGE permission to your AndroidManifest.xml.

Add the following to your android/app/src/main/AndroidManifest.xml inside the <manifest> tag:

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

Usage #

Import the package: #

import 'package:easy_file_saver/easy_file_saver.dart';

Saving a file #

You can save a file using the EasyFileSaver.save method. This method requires the fileName, bytes (as Uint8List), and the directory (EasyFileSaveDirectory enum) where the file should be saved.

import 'dart:typed_data';
import 'package:flutter/material.dart';
import 'package:easy_file_saver/easy_file_saver.dart';

// Example of saving an image file
Future<void> saveImageFile(BuildContext context, Uint8List imageBytes) async {
  String fileName = "my_image.png"; // Include extension
  try {
    String? filePath = await EasyFileSaver.save(
      fileName: fileName,
      bytes: imageBytes,
      directory: EasyFileSaveDirectory.external, // Example: save to external media
      onPermissionDenied: () {
        // Handle permission denied case
        ScaffoldMessenger.of(context).showSnackBar(
          SnackBar(content: Text('Storage permission denied!')),
        );
      },
    );
    if (filePath != null) {
      ScaffoldMessenger.of(context).showSnackBar(
        SnackBar(content: Text('Image saved to: $filePath')),
      );
    } else {
      // For SAF, filePath will be null as it doesn't return a direct path
      ScaffoldMessenger.of(context).showSnackBar(
        SnackBar(content: Text('File saved successfully (path not returned for SAF)')),
      );
    }
  } catch (e) {
    ScaffoldMessenger.of(context).showSnackBar(
      SnackBar(content: Text('Failed to save image: $e')),
    );
  }
}