如何将PDF文件保存在扑波中?

发布于 2025-01-29 08:04:19 字数 837 浏览 2 评论 0原文

我有一个PDF文件。我想在手机内存中写入该文件?以下是代码。 首先,我正在扫描,然后将图像转换为PDF,现在我想将PDF保存到电话内存。请帮我。

  void onDocumentScanner(BuildContext context) async {
try {
  File scannedDocumentFile;
  var doc = await DocumentScannerFlutter.launchForPdf(context);
  if (doc != null) {
    refreshDownloadedFiles();
    scannedDocumentFile = doc;
    String fileName = basename(scannedDocumentFile.path);
    final directory = (await getExternalStorageDirectory())!.path;
    File saveFilePath = File('$directory/$fileName');
    saveFilePath.openWrite(); //Here I want to save to the file

    print("Path = $fileName");
    Get.toNamed(AppRoutes.viewDownloadedFile,
            arguments: [scannedDocumentFile.path, fileName, folderId])!
        .whenComplete(() {
      refreshFetchedData();
    });
  }
} catch (e) {
  print(e);
}

}

I have a pdf file. I want to write that file in phone memory? Below is the code.
First I am scanning then I am converting the image to pdf, Now I want to save that pdf to phone memory. Please help me.

  void onDocumentScanner(BuildContext context) async {
try {
  File scannedDocumentFile;
  var doc = await DocumentScannerFlutter.launchForPdf(context);
  if (doc != null) {
    refreshDownloadedFiles();
    scannedDocumentFile = doc;
    String fileName = basename(scannedDocumentFile.path);
    final directory = (await getExternalStorageDirectory())!.path;
    File saveFilePath = File('$directory/$fileName');
    saveFilePath.openWrite(); //Here I want to save to the file

    print("Path = $fileName");
    Get.toNamed(AppRoutes.viewDownloadedFile,
            arguments: [scannedDocumentFile.path, fileName, folderId])!
        .whenComplete(() {
      refreshFetchedData();
    });
  }
} catch (e) {
  print(e);
}

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

病毒体 2025-02-05 08:04:19

我之前从未看过.openwrite()方法,但是文档说它具有2个命名参数filemode和编码 - 尝试指定它们。
如果它不起作用,我只能与.writeasbytes方法共享我的解决方案。
代替SaveFilePath.openWrite()您可以先捆绑数据,然后保存。

final byteData = await rootBundle.load(fileName);
await saveFilePath.writeAsBytes(byteData.buffer
          .asUint8List(byteData.offsetInBytes, byteData.lengthInBytes));

I haven't seen the .openWrite() method before, but the documentation says that it has 2 named arguments FileMode and encoding - try to specify them.
If it won't work, I can only share my solution, with .writeAsBytes method.
Instead of saveFilePath.openWrite() you can bundle the data first and then save it.

final byteData = await rootBundle.load(fileName);
await saveFilePath.writeAsBytes(byteData.buffer
          .asUint8List(byteData.offsetInBytes, byteData.lengthInBytes));

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文