dpa99c / cordova-diagnostic-plugin

Cordova/Phonegap plugin to manage device settings
539 stars 361 forks source link

Android SDK 33 - Permission WRITE_EXTERNAL_STORAGE not supported for build SDK version 31 #518

Closed drahuks closed 1 month ago

drahuks commented 5 months ago

Bug report

Current behavior:

I have an ionic 7/cordova (12.0.0) application using cordova-plugin-camera 7.0.0 and cordova-diagnostic-plugin 7.1.4

App is built with android-targetSdkVersion=33

I manage permissions using diagnostic plugin before calling camera plugin, using getCameraAuthorizationStatus/requestCameraAuthorization. I need the storage permission as I want to save taken pictures to user Photo Album

I get error reports from users running app on Android 12 that are not able to take pictures The requestCameraAuthorization call gives following error :

Permission WRITE_EXTERNAL_STORAGE not supported for build SDK version 31

From my readings I got that theREAD/WRITE_EXTERNAL_STORAGE permissions were not effective anymore starting from sdk 33, but that they were still accepted in lower versions. That's what is done in the permission plugin source code

Is there something I am doing wrong here?

Thanks for helping me understand this behaviour...

Expected behavior: Permissions should be handled well in android 12

Environment information

Runtime issue

Android build issue:

Related code:

if(this.platform.is('android') && !this.diagnostic.isRequestingPermission()){
        console.log('Request file storage access');
        (<any>window).cordova.plugins.diagnostic.getCameraAuthorizationStatus(async (status) => {
          console.log('getCameraAuthorizationStatus:'+status);
          if(status === (<any>window).cordova.plugins.diagnostic.permissionStatus.GRANTED){
             console.log("Camera use is authorized");
             this.callTakePicture(record);
          }
          else if(status !== (<any>window).cordova.plugins.diagnostic.permissionStatus.DENIED_ALWAYS){
            this.callTakePicture(record);
            //permissions are asked in camera plugin
            (<any>window).cordova.plugins.diagnostic.requestCameraAuthorization(success => {
              console.log("Authorization request for camera use was " + (success == (<any>window).cordova.plugins.diagnostic.permissionStatus.GRANTED ? "granted" : "denied"));
              if(success == (<any>window).cordova.plugins.diagnostic.permissionStatus.GRANTED){
                this.takePicture(record);
              }
            }, error => {
                console.error('error:'+error);
            }, true);
          }
          else{
            await this.alertMissingPermission();
            //this.callTakePicture(record);
          }
        }, async (error) => {
          console.log('Request permission error:'+error);
          this.callTakePicture(record);
        }, {externalStorage: true});
      }  
      else{
        this.callTakePicture(record);
      }

Console output

console output ``` (error:Exception occurred: Permission WRITE_EXTERNAL_STORAGE not supported for build SDK version 31) ```


**Other information:** none
drahuks commented 5 months ago

Ok, I see there is a conflict between the camera storage permission selection in Diagnostic_Camera.java:

protected static String[] storagePermissions; static { if (android.os.Build.VERSION.SDK_INT >= 33) { // Build.VERSION_CODES.TIRAMISU / Android 13 storagePermissions = new String[]{ "READ_MEDIA_IMAGES", "READ_MEDIA_VIDEO" }; } else { storagePermissions = new String[]{ "READ_EXTERNAL_STORAGE", "WRITE_EXTERNAL_STORAGE" }; } }

and the permission max SDK check in Diagnostic.java:

`/**

So devices running SDK 30/31 will request WRITE_EXTERNAL_STORAGE but permission request will throw an error as current SDK is not compatible

mirko77 commented 3 months ago

@drahuks how did you solve this? We are facing the same issues on Android 11 devices

drahuks commented 3 months ago

@mirko77 You need to edit the plugin source code Diagnostic.java, line 190: Diagnostic.addBiDirMapEntry(_permissionsMap, "WRITE_EXTERNAL_STORAGE", 29);

Set api level 32 instead of 29

dpa99c commented 1 month ago

This should be fixed in plugin v7.2.0 which was just published