Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter: Permission denied writing file

Tags:

io

flutter

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);
like image 236
Jus10 Avatar asked Jun 16 '18 22:06

Jus10


3 Answers

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.

like image 199
Munjata KETA Avatar answered Oct 20 '22 11:10

Munjata KETA


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/

like image 7
Matt S. Avatar answered Oct 20 '22 10:10

Matt S.


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

 <application
      android:requestLegacyExternalStorage="true"
like image 7
Rajil TL Avatar answered Oct 20 '22 10:10

Rajil TL