我正在尝试在我的设备上保存 pdf,但出现此错误
FileSystemException:无法打开文件,路径='目录:'/storage/emulated/0/Android/data/esofos.health/files'/test.pdf
这是生成文档的函数
_generatepdf() async {
//Get external storage directory
final directory = await getExternalStorageDirectory();
//Get directory path
final path = directory;
// Create a new PDF document.
final PdfDocument document = PdfDocument();
// Add a PDF page and draw text.
document.pages.add().graphics.drawString(
'Hello World!', PdfStandardFont(PdfFontFamily.helvetica, 12),
brush: PdfSolidBrush(PdfColor(0, 0, 0)),
bounds: const Rect.fromLTWH(0, 0, 150, 20));
// Save the document.
print(path);
File('$path/test.pdf').writeAsBytes(document.save());
// Dispose the document.
document.dispose();
};
使用
Directory
path 属性而不是类的字符串表示形式。像这样的东西:
File('${directory.path}/test.pdf').writeAsBytes(document.save());
如果不是需要访问设备外部存储的应用程序(例如防病毒应用程序),并且您想在 Flutter 中下载文件,您可以在安装过程中使用包 path_provider 来使用您的应用程序特定目录提供程序。用它来下载文件
final response = await http.get(Uri.parse(URL!));
final appDir = await getApplicationDocumentsDirectory();//use this please instead of getExternalStorageDirectory
File file = File(appDir.path + '/${filename}');
try {
await file.writeAsBytes(response.bodyBytes);
print('success');
} catch (e) {
print(e.toString());
}
检查提供的代码片段时,目录路径的类型转换不正确。我们要求您更改以下代码以正确保存和打开 pdf 文件。
final path = directory!.path;
请参阅以下文档链接,
UG:https://help.syncfusion.com/flutter/pdf/getting-started#save-and-open-a-pdf-document-in-mobile