Search code examples
androidreact-nativeionic-frameworkionic-native

Ionic React Native - There was an error with the request: /storage/emulated/0/Download/newPicture.png: open failed: EACCES (Permission denied)


I am facing issues with my Ionic React Android App. I want to download a file to the Public Downloads Folder of Android, but I am always getting this error:

Msg: error block ...  There was an error with the request: /storage/emulated/0/Download/newPicture.png: open failed: EACCES (Permission denied)

I am using following code:

function download(name){
    AndroidPermissions.hasPermission(AndroidPermissions.PERMISSION.READ_EXTERNAL_STORAGE)
        .then(status => {
          if (status.hasPermission) {
            console.log("Has permission");
            performAndroidDownload(name);
          } 
          else {
            console.log("Request permission");
            AndroidPermissions.requestPermission(AndroidPermissions.PERMISSION.READ_EXTERNAL_STORAGE)
              .then(status => {
                if(status.hasPermission) {
                  performAndroidDownload(name);
                }
              });
          }
        });
}

    function performAndroidDownload(name:string){
        const filePath = File.externalRootDirectory +"Download/"+ name; 
        console.log(filePath);
        HTTP.downloadFile("https://url.com/uploads/"+match.params.id+"/"+name, {}, {}, filePath).then(response => {
        // prints 200
        console.log('success block...', response);
        }).catch(err => {
        // prints 403
        console.log('error block ... ', err.status);
        // prints Permission denied
        console.log('error block ... ', err.error);
        });
    }

I also added android:requestLegacyExternalStorage="true" to my AndroidManifest.xml, but it didn´t help.

Tried the WRITE_EXTERNAL_STORAGE permission as well, but didn´t help.

Thank you for your help!


Solution

  • I finally solved the permission issue!

    After requesting the permission like above, I needed to add following line:

    AndroidPermissions.requestPermissions([AndroidPermissions.PERMISSION.READ_EXTERNAL_STORAGE,AndroidPermissions.PERMISSION.WRITE_EXTERNAL_STORAGE])
    

    The whole solution is:

    function download(name){
        AndroidPermissions.hasPermission(AndroidPermissions.PERMISSION.READ_EXTERNAL_STORAGE)
            .then(status => {
              if (status.hasPermission) {
                console.log("Has permission");
                performAndroidDownload(name);
              } 
              else {
                console.log("Request permission");
                AndroidPermissions.requestPermission(AndroidPermissions.PERMISSION.READ_EXTERNAL_STORAGE)
                  .then(status => {
                    if(status.hasPermission) {
                      performAndroidDownload(name);
                    }
                  });
              }
            });
    }
    
    function performAndroidDownload(name:string){
        const filePath = File.externalRootDirectory + "Download/" + name; 
        AndroidPermissions.requestPermissions([AndroidPermissions.PERMISSION.READ_EXTERNAL_STORAGE,AndroidPermissions.PERMISSION.WRITE_EXTERNAL_STORAGE]).then(e => {
          
          HTTP.downloadFile("https://url.com/uploads/"+match.params.id+"/"+name, {}, {}, filePath).then(response => {
            // prints 200
            console.log('success block...', JSON.stringify(response));
        
            }).catch(err => {
            // prints 403
            console.log('error block ... ', err.status);
            // prints Permission denied
            console.log('error block ... ', err.error);
            });
        });
    
      }