Flutter - Save file visible to the user

梦想与她 提交于 2020-01-25 08:22:49

问题


In a Flutter project I create a pdf document. I can save the document in the path of the app. But the user has no access to it. Alternatively, how can I save the file to another folder where the user sees it?

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);
  }

回答1:


if you are using this app only on Android, you can try using:

Future<Directory> getExternalStorageDirectory()

from the path_provider plugin.

This method is used to get to the top and publically accessible directory in the storage. From here you can add subdirectory path as /Downloads/your_file_name.

This should probably help you. If it doesn't, add a comment and let me know.

The method's source code 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)`.



回答2:


Use downloads_path_provider, on android it will save the file on Downloads.

for IOS, you need to use getApplicationDocumentsDirectoryfrom path_provider and add permissions on info.plist to make the folder visible to the user:

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


来源:https://stackoverflow.com/questions/59154031/flutter-save-file-visible-to-the-user

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!