【问题标题】:Save a text file from a Flutter Android app that can be accessed by user保存用户可以访问的 Flutter Android 应用程序中的文本文件
【发布时间】:2020-01-13 00:25:08
【问题描述】:

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

我正在尝试:

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

在 Android 9 手机上,这会将文件保存在:

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

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

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

谁能指出我正确的方向?

【问题讨论】:

  • 将其保存到/storage/emulated/0/your_app_name/test1.txt 或应用数据之外的任何位置。
  • @Ryosuke 你在 Flutter 中做到了吗?对我来说,它只允许检查文件或目录是否存在,但其他任何内容都被拒绝,并具有正确的权限集。
  • @creativecreatorormaybenot 现在你提到我记得这也发生在我身上,然后我通过MethodChannel 做到了......但我在回复你的评论之前再次检查了它,它工作得非常好。
  • @Ryosuke 您可以使用什么功能(来自 path_provider 插件或其他地方)来获取该路径(“外部应用程序数据”)?
  • @AlexVang 在几乎所有安卓设备中都是一样的。

标签: android file flutter save


【解决方案1】:

我有一个类似的用例,只是我想在 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"/>
    ...
  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);
  }

  1. 在手机上运行应用程序
  2. 为您的应用启用“存储”权限

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

希望有效果

【讨论】:

    猜你喜欢
    • 2021-08-17
    • 2020-03-27
    • 1970-01-01
    • 2016-03-24
    • 1970-01-01
    • 1970-01-01
    • 2011-12-04
    • 2020-10-10
    • 2013-07-18
    相关资源
    最近更新 更多