Flutter-保存文件对用户可见

问题描述 投票:0回答:1

在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);
  }
file pdf flutter storage store
1个回答
0
投票

如果您仅在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)`.
© www.soinside.com 2019 - 2024. All rights reserved.