user2244131
user2244131

Reputation: 83

Saving files locally so available to email or view in file manager Android

Flutter, dart and Visual Studio. Android Devices.

After saving a file and reading it back to ensure all went OK I can't find the file, so it can't be emailed or viewed at all.

How can I save it so the user can see the file, so they can read it, or email it etc.

Thanks

class SaveJson {
  SaveJson();

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

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

  Future<File> writeJson(String data) async {
    final file = await _localFile;
    return file.writeAsString(data);
  }

  read() async {
    try {
      final directory = await getApplicationDocumentsDirectory();
      final file = File('${directory.path}/macf.txt');
      String text = await file.readAsString();
      print(text);
    } catch (e) {
      print("Couldn't read file");
    }
  }
}

Upvotes: 2

Views: 610

Answers (1)

Arash Mohammadi
Arash Mohammadi

Reputation: 1419

You are using the path_provider package and The getApplicationDocumentsDirectory returns the path to the applications hidden directory. Users can't see this directory with file managers (There is a way to find it but that is not easy for end user). You can use the downloads_path_provider package:

import 'package:downloads_path_provider/downloads_path_provider.dart';  

Future<Directory> downloadsDirectory = DownloadsPathProvider.downloadsDirectory;

In this code, the downloadsDirectory is the Downloads directory in phone storage, that can be seen by user. Then you can open or email it.

downloads_path_provider package link

Upvotes: 3

Related Questions