mvandak commented on issue #613:
URL: 
https://github.com/apache/cordova-plugin-file/issues/613#issuecomment-1842283106

   We had the same problem with writing to our own directory in Android root 
dir (`cordova.file.externalRootDirectory`) starting with Android 13.
   
   As far as I understood from the Android documentation, 
MANAGE_EXTERNAL_STORAGE is enough to write to that directory. This permission 
was implemented in one of previous Android versions. However, this permission 
has to be granted exclusively by user, it is not enough to be mentioned in 
config.xml.
   We managed it in our application by checking permission at app start and if 
not granted, app starts intent to request permission to be granted by user. 
This we had done already some time ago in previous version of Android.
   
   But, starting with Android 13 we were not able to write. I realized that the 
cordova-file-plugin is refusing get dir request because we had have not granted 
all READ_MEDIA_* permissions and FileUtils.hasReadPermission() returned false. 
If I added READ_MEDIA_* permissions to config.xml everything worked as before.
   
   Anyway, I don't understand why filePlugin is testing if all READ_MEDIA_* 
permissions are granted to allow read access and not at least one of. IMHO, 
this is bypassing the Android granularity.
   And also MANAGE_EXTERNAL_STORAGE should be taken into account when testing 
read/write permissions.
   
   I propose to test in hasReadPermission and also in hasWritePermission if all 
files access is granted also. If it is granted, everything works without 
READ_MEDIA_* permissions granted. In case, I disabled All files access in 
Android Special access settings, read/write was refused with Unknown error from 
filePlugin
   I suggest these methods to be modified in FileUtils as follows:
   
   ```java
    private boolean hasReadPermission() {
           if (android.os.Build.VERSION.SDK_INT >= 
Build.VERSION_CODES.TIRAMISU) {
               return Environment.isExternalStorageManager() // Test all files 
access, MANAGE_EXTERNAL_STORAGE permission
                       || PermissionHelper.hasPermission(this, 
Manifest.permission.READ_MEDIA_IMAGES)
                       || PermissionHelper.hasPermission(this, 
Manifest.permission.READ_MEDIA_VIDEO)
                       || PermissionHelper.hasPermission(this, 
Manifest.permission.READ_MEDIA_AUDIO);
           } else {
               return PermissionHelper.hasPermission(this, 
Manifest.permission.READ_EXTERNAL_STORAGE);
           }
       }
   
   private boolean hasWritePermission() {
       // Starting with API 33, requesting WRITE_EXTERNAL_STORAGE is an auto 
permission rejection
       return android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU
               ? Environment.isExternalStorageManager()   // Test all file 
access, MANAGE_EXTERNAL_STORAGE permission
               : PermissionHelper.hasPermission(this, 
Manifest.permission.WRITE_EXTERNAL_STORAGE);
   }
   ```


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscr...@cordova.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscr...@cordova.apache.org
For additional commands, e-mail: issues-h...@cordova.apache.org

Reply via email to