Flutter: Permission denied writing file

16,878

Solution 1

There may be a problem with the paths depending on the device being used. Have a look at the path_provider plugin: https://pub.dartlang.org/packages/path_provider

There's a good write-up of how to read and write files in the Flutter Cookbook on flutter.io: https://flutter.io/cookbook/persistence/reading-writing-files/

Solution 2

In Android Q, just add the Following Lines in AndroidManifest file:

 <application
      android:requestLegacyExternalStorage="true"

Solution 3

final Io.Directory systemTempDir = Io.Directory.systemTemp; seems to not work in release mode.

I had to do as below:

Directory tempDir = await getTemporaryDirectory();
final File file = File("${tempDir.path}/$fileName");

getTemporaryDirectory() is provided by path_provider.

Share:
16,878
Jus10
Author by

Jus10

Updated on December 05, 2022

Comments

  • Jus10
    Jus10 over 1 year

    I am having a problem writing a file in Flutter. I keep getting this error:

    FileSystemException: Cannot create file, path = '/data/local/tmp/temp.png' (OS Error: Permission denied, errno = 13)

    For some reason, it's only happening on some devices. I can't seem to duplicate the problem myself, but people are reporting it to me.

    Here's the basic code:

    final Io.Directory systemTempDir = Io.Directory.systemTemp;
    final Io.File file = await new Io.File('${systemTempDir.path}/temp.png').create();
    file.writeAsBytes(finalImage);
    
  • Jus10
    Jus10 almost 6 years
    That did it! Imported path_provider and changed Io.Directory.systemTemp; to getTemporaryDirectory() and it works! Thank you!
  • Kamlesh
    Kamlesh over 3 years
    I am trying to access /storage/emulated/ directory but getting error "Unhandled Exception: FileSystemException: Directory listing failed, path = '/storage/emulated/' (OS Error: Permission denied, errno = 13)" while i have granted WRITE_EXTERNAL_STORAGE and READ_EXTERNAL_STORAGE permission. Any suggestion. Thanks.
  • Nithin Sai
    Nithin Sai over 3 years
    Hey, the build fails if I add this and the minSdkVersion is below 29
  • Hyung Tae Carapeto Figur
    Hyung Tae Carapeto Figur almost 3 years
    @Kamlesh Same here. Could you fix your problem?