Home > Back-end >  FileSystemException: Cannot open file, path = 'Directory: '/storage/emulated/0/Android/dat
FileSystemException: Cannot open file, path = 'Directory: '/storage/emulated/0/Android/dat

Time:06-22

I'm trying to save a pdf on my device , but i got this error

FileSystemException: Cannot open file, path = 'Directory: '/storage/emulated/0/Android/data/esofos.health/files'/test.pdf

this is the function that generate the document

_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();

};

CodePudding user response:

Use Directory path property not string representation of class. Something like this:

File('${directory.path}/test.pdf').writeAsBytes(document.save());

CodePudding user response:

When checking the provided code snippet, the directory path was not type casted properly. We request you to change the below code to save and open the pdf file properly.

final path = directory!.path;

Please refer to the below documentation link,

UG: https://help.syncfusion.com/flutter/pdf/getting-started#save-and-open-a-pdf-document-in-mobile

Note: I work for Syncfusion.

  • Related