Save a text file from a Flutter Android app that can be accessed by user

2,004

I had a similar use-case except I wanted to save "Downloads" folder in Android. Here's what worked for me:

  1. Add Permissions to AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.app">

    <!-- Need this permission so that other apps see the saved files -->
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    ...
  1. Function for saving your file
    NOTE: that I used downloads_path_provider instead of "path_provider"
  static Future<void> save(String filename, String content) async {
    final directory = await DownloadsPathProvider.downloadsDirectory;
    final path = '${directory.path}/$filename';
    final file = File(path);
    Logger().d("Saving $path");
    await file.writeAsString(content);
  }

  1. Run the App on your phone
  2. Enable "Storage" permission for your app AppPermissions

  3. Save the file? I was able to view the file saved using a File Browser without being rooted or anything.

Hopefully that works

Share:
2,004
TechAurelian
Author by

TechAurelian

Updated on December 14, 2022

Comments

  • TechAurelian
    TechAurelian over 1 year

    The user of my Android app developed in Flutter should be able to save (export) some data to a text file. The user should be able to find and access this file on his Android device using other apps (including File Managers).

    I'm trying:

    final directory = await getExternalStorageDirectory();
    final file = File('${directory.path}/test1.txt');
    await file.writeAsString('Test');
    

    On an Android 9 phone this saves the file in:

    /storage/emulated/0/Android/data/com.mydomain.myapp/files
    

    However, I can't access the file (or the directory) using a File Manager app (e.g. Files) on the phone.

    (If I connect the phone to a Windows PC using a USB cable, I can access the directory, see the test1.txt file, but I can't view or access it.)

    Can anyone point me in the right direction?