Flutter-保存文件对用户可见

在Flutter项目中,我创建了一个pdf文档。我可以将文档保存在应用程序的路径中。 但是用户无法访问它。另外,如何将文件保存到用户可以看到的另一个文件夹中?

import 'package:path_provider/path_provider.dart';
  Future<void> savePdfDocument() async {
    final PdfCreater generatedPdf = PdfCreater(document);
    final List<int> generatedPdfDocument = generatedPdf.buildPdf();

    final String dir = (await getapplicationDocumentsDirectory()).path;
    final String path = '$dir/example.pdf';
    final File file = File(path);
    file.writeAsBytesSync(generatedPdfDocument);
  }
skywwh123 回答:Flutter-保存文件对用户可见

使用downloads_path_provider,在android上,它将文件保存在Downloads上。

对于IOS,您需要使用path_provider中的getApplicationDocumentsDirectory并在info.plist上添加权限以使该文件夹对用户可见:

<key>LSSupportsOpeningDocumentsInPlace</key>
<true/>
<key>UIFileSharingEnabled</key>
<true/>
,

如果仅在Android上使用此应用,则可以尝试使用:

Future<Directory> getExternalStorageDirectory()

来自path_provider插件。

此方法用于访问存储中的顶部目录和可公共访问的目录。 在这里,您可以将子目录路径添加为/Downloads/your_file_name

这可能会对您有所帮助。如果没有,请添加评论,让我知道。

方法的源代码documentation

/// Path to a directory where the application may access top level storage.
/// The current operating system should be determined before issuing this
/// function call,as this functionality is only available on Android.
///
/// On iOS,this function throws an [UnsupportedError] as it is not possible
/// to access outside the app's sandbox.
///
/// On Android this uses the `getExternalFilesDir(null)`.
本文链接:https://www.f2er.com/2987137.html

大家都在问