保存用户可以访问的 Flutter Android 应用程序中的文本文件

Ale*_*ang 5 android file save flutter

我在 Flutter 中开发的 Android 应用程序的用户应该能够将一些数据保存(导出)到文本文件中。用户应该能够使用其他应用程序(包括文件管理器)在其 Android 设备上找到并访问此文件。

我想:

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

在 Android 9 手机上,该文件保存在:

/storage/emulated/0/Android/data/com.mydomain.myapp/files
Run Code Online (Sandbox Code Playgroud)

但是,我无法使用手机上的文件管理器应用程序(例如文件)访问文件(或目录)。

(如果我使用 USB 电缆将手机连接到 Windows PC,我可以访问目录、查看文件test1.txt,但无法查看或访问它。)

有人能指出我正确的方向吗?

Ara*_*adi 2

我有一个类似的用例,除了我想在 Android 中保存“下载”文件夹。这对我有用:

  1. 添加权限到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"/>
    ...
Run Code Online (Sandbox Code Playgroud)
  1. 保存文件的函数
    注意:我使用downloads_path_provider而不是“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);
  }

Run Code Online (Sandbox Code Playgroud)
  1. 在手机上运行应用程序
  2. 为您的应用启用“存储”权限 应用权限

  3. 保存文件吗?我能够查看使用文件浏览器保存的文件,而无需 root 或任何其他操作。

希望这有效