Search code examples
androidflutterandroid-permissionsandroid-external-storage

Flutter Android - Write files to external storage


I am unable to write a file to the external storage (e.g. /storage/emulated/0/Example).

My current configuration looks as follows:

  • targetSdk: 33
  • WRITE_EXTERNAL_STORAGE and READ_EXTERNAL_STORAGE added
  • "storage" permission request at runtime (Permission.storage.request())

With this configuration, I always get a "permanently denied" when checking the storage permission on runtime. Therefore I added the following to AndroidManifest:

  • android:requestLegacyExternalStorage="true"

With this change, I get a permission request window at runtime and I can confirm that my app can use this permission.

Now the permission check returns "granted", but if I try to write a file e.g. /storage/emulated/0/Example/test.txt I get an error:

[ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: FileSystemException: Cannot open file, path = '/storage/emulated/0/Example/test.txt' (OS Error: Operation not permitted, errno = 1)

Then I tried it with the Granular Media Permissions for Android 13:

  • READ_MEDIA_IMAGES, READ_MEDIA_AUDIO, READ_MEDIA_VIDEO

This also did not work for me.

The only way I got this working is by defining the MANAGE_EXTERNAL_STORAGE permission. But if I upload my appbundle Google rejects it because of this permission.

What other options do I have for this requirements:

  • Flutter
  • targetSdk > 33
  • allow user to save files to any folder he wants
  • do not use MANAGE_EXTERNAL_STORAGE

Thank you!


Solution

  • You cannot create files or directories in root of external storage anymore.

    Use one of the public directories like Documents, Download, DCIM, Pictures, ...