terikon / cordova-plugin-photo-library

Maintainer needed. Please contact if you're using this library in your project
MIT License
149 stars 295 forks source link

Getting "open failed: EACCES permission denied" even I have permissions. Android 10 #196

Closed eddsaura closed 2 years ago

eddsaura commented 3 years ago

Good evening cordova fellas,

We are having some problems with photolibrary at saving an image.

I thought it was the way of asking for the permissions by photolibrary.

At first I had this:

this.photoLibrary
              .requestAuthorization({ read: true, write: true })
              .then(() => {
                this.photoLibrary
                  .saveImage(dataUrl, 'amazingApp')
                  .then(res => {
                    loading.dismiss()
                    $('ion-fab, .indicator').css('opacity', '1')
                  }).catch(err => {
                    window.alert(err)
                    // WE ARE GETTING THE ERROR HERE
                    loading.dismiss()
                    $('ion-fab, .indicator').css('opacity', '1')
                  })
              })
              .catch(err => {
                window.alert(err)
                loading.dismiss()
                $('ion-fab, .indicator').css('opacity', '1')
                alert("You don't have permission the save images")
              })

So I decided to use AndroidPermissions to try to fix this, so now, when I start the app it just checks the permissions. In case we don't have android.permission.WRITE_EXTERNAL_STORAGE we just ask for them. Ok...

This is the function:

requestAndroidPermissions () {
    if (this.plt.is('android')) {
      window.alert('Yes is an android')
      this.androidPermissions.checkPermission(this.androidPermissions.PERMISSION.WRITE_EXTERNAL_STORAGE).then(
        result => {
          window.alert('I HAVE PERMISSIONS!!')
          window.alert(result.hasPermission)
          if(!result.hasPermission){
            this.androidPermissions.requestPermission(this.androidPermissions.PERMISSION.WRITE_EXTERNAL_STORAGE)
          }
        },
        err => {
          window.alert(' NO PERMISSIONS, IM GONNA REQUEST: ')
          this.androidPermissions.requestPermission(this.androidPermissions.PERMISSION.WRITE_EXTERNAL_STORAGE)
        }
      )
    }
  }

I wrote this function with WRITE_EXTERNAL_STORAGE because here in the repo you suggest to add this to the config.xml. Thing is that I can't make it work this way, I don't know why.

<platform name="android">
  <config-file target="AndroidManifest.xml" parent="/*">
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
  </config-file>
</platform>

Ok so.. Any idea?

Thanks a lot!

drop0118 commented 3 years ago

Add this in config.xml `

    </edit-config>`

I solved it after adding this code

eddsaura commented 3 years ago

Add this in config.xml <edit-config file="app/src/main/AndroidManifest.xml" mode="merge" target="/manifest/application" xmlns:android="http://schemas.android.com/apk/res/android"> <application android:requestLegacyExternalStorage="true" /> </edit-config> I solved it after adding this code

Hey, thanks for replying. There is one problem with my post, I had Cordova 9 with Android 8, aparently the requestLegacyExternalStorage can only be used with Cordova 10 and Android 9, and also we had to install a plugin called cordova-androidx-support or something like that, after all of that we were allowed to use requestLegacyExternalStorage into the manifest!

Now it is working with my android10, now I have to test with older smartphones hehehe....

VrVkr commented 2 years ago

Add this in config.xml <edit-config file="app/src/main/AndroidManifest.xml" mode="merge" target="/manifest/application" xmlns:android="http://schemas.android.com/apk/res/android"> <application android:requestLegacyExternalStorage="true" /> </edit-config> I solved it after adding this code

Hey, I am facing the same issue too I have tried this but still I am facing the same error while uploading a file to my server. Can you send me the steps clearly so that I can check or update you whether I have done in the right way or not. Thanks in advance

israeltrejo commented 2 years ago

I changed the android version in which the application is focused adding this tag in the config.xml:

And this change allows to save files into the device using on Android 8, Android 9, Android 10 and Android 11. I did tests on Android emulator and physical devices working correctly.

The Android documentation recommends to change the API Level version to avoid the scoped storage feature on Android 10.

You get more information about scoped storage feature here: https://developer.android.com/training/data-storage/use-cases#opt-out-scoped-storage

I hope this information will be helpful.