Home > Mobile >  Flutter - Local files importation
Flutter - Local files importation

Time:08-22

i would like to import in my flutter application, files from "Files" and "Photos" default ios/android/macos applications.

Have you got any solution to do that ?

Thank you

CodePudding user response:

See this documentation: https://docs.flutter.dev/cookbook/persistence/reading-writing-files

add to pubspec (https://pub.dev/packages/path_provider):

path_provider: ^2.0.11

import package:

import 'package:path_provider/path_provider.dart';

Find local path:

Future<String> get _localPath async {
  final directory = await getApplicationDocumentsDirectory();

  return directory.path;
}

create reference:

Future<File> get _localFile async {
  final path = await _localPath;
  return File('$path/counter.txt');
}

read data:

Future<int> readCounter() async {
  try {
    final file = await _localFile;

    // Read the file
    final contents = await file.readAsString();

    return int.parse(contents);
  } catch (e) {
    // If encountering an error, return 0
    return 0;
  }
}

CodePudding user response:

You can use image_picker package to access user's photos with the required permissions;

CodePudding user response:

You can use the flutter File Picker package.

https://pub.dev/packages/file_picker

  • Related