skip to Main Content

I’m trying to create a new JSON file generated by my Flutter application and I wanna let the user to choose where to store this file.

I’m currently using file_picker package to let the user to choose the path to store the generated file. If I select the Documents folder (/storage/emulated/0/Documents) I can create that file, but if I choose another path (like /storage/emulated/0/DCIM or a custom folder created by the user named Test for example) I get the following exception:
FileSystemException: Cannot open file, path = '/storage/emulated/0/Test/backup.json' (OS Error: Operation not permitted, errno = 1)

Note that I’m running my app on an emulator that have Android 13. I also tried to add this permissions in the Manifest:

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

And tried to request storage permissions with the package permission_handler but nothing change.

2

Answers


  1. DCIM maybe is part from media or photos

    so try this if work, I do usually include in my manifest

    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
     // try to add this 2 lines
    <uses-permission android:name="android.permission.READ_MEDIA_IMAGES"/>
    <uses-permission android:name="android.permission.READ_MEDIA_VIDEO"/>
     // Optional to add this 
     <uses-permission android:name="android.permission.ACCESS_MEDIA_LOCATION"/>
    

    and setting my gradle as like this

      android {
         compileSdkVersion 33
         ....
         // from deafult config 
         defaultConfig{
          ...
          mindSdkVersion 28
          targetSdkVersion 33
    
      }
    

    then for permission you can either put them on list just in case when requesting

     // You can either call this in initState or any listener you have
     // as long ass it can be triggered first on page
     static Future<void> requestStorageorPhotos() async{
        await [
           Permission.photos,
           Permission.storage,
         ].request();
        // try to listen from its changes permission
        if((await Permission.photos.status.isGranted) || (await 
          Permission.storage.status.isGranted)){
          // Do your thing if allow
        }else{
         // or anything in here from not allowing it.
        }
      }
    
    Login or Signup to reply.
  2. If you are targeting Android 11 and up(targetSdkVersion 30) then you require the following permissions in AndroidManifest.xml for modifying and document access.

    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"
        android:maxSdkVersion="28" />
    <uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" />
    

    You can find more details in here
    https://stackoverflow.com/a/66366102/19292778

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search