Search code examples
androidionic-frameworkionic2filechooser

open failed: EACCES (Permission denied) in ionic -2


I was using cordova-plugin-file-transfer and cordova-plugin-file-chooser on Ionic mobile application to upload some files to Android device which can open with native applications like pdf, word, excel..etc

It is now throwing "exception":"/storage/emulated/0/Download/pdf-test.pdf: open failed: EACCES (Permission denied)"}".

openFile() {
    console.log("openFile");

        this.fileChooser.open()
      .then(uri => {
        console.log(uri)

        const fileTransfer: FileTransferObject = this.transfer.create();

        let options: FileUploadOptions = {
          fileKey: 'files',
          fileName: 'name.doc',
           params: { resume: uri},
          chunkedMode: false,
          headers: {
            'token': this.token,
            'sid': this.sid,
            'user': this.user,
            'to': this.to,
            'node': this.node,
            'type': 'doc',
            'cap': this.cap
          }

        }
        console.log("Headers" + JSON.stringify(options));

        fileTransfer.upload(uri,this.apiurl, options)
          .then((data) => {
            // success
            console.log("Response data ->>>>>>> :- " + JSON.stringify(data));
            alert("success" + JSON.stringify(data));
          }, (err) => {
            // error
            console.log("Error data ->>>>>>> :- " + JSON.stringify(err));
            alert("error" + JSON.stringify(err));
          });

      })
      .catch(e => console.log(e));
  }

I also added permission on AndroidManifest.xml

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

Solution

  • Android Permissions

    This plugin is designed for supporting Android new permissions checking mechanism.

    Since Android 6.0, the Android permissions checking mechanism has been changed. In the past, the permissions were granted by users when they decide to install the app. Now, the permissions should be granted by a user when he/she is using the app.

    For old Android plugins you (developers) are using may not support this new mechanism or already stop updating. So either to find a new plugin to solving this problem, nor trying to add the mechanism in the old plugin. If you don't want to do those, you can try this plugin. https://ionicframework.com/docs/native/android-permissions/

    filePermission() {
            this.androidPermissions.checkPermission(this.androidPermissions.PERMISSION.WRITE_EXTERNAL_STORAGE).then(
              result => console.log('Has permission?', result.hasPermission),
              err => this.androidPermissions.requestPermission(this.androidPermissions.PERMISSION.WRITE_EXTERNAL_STORAGE)
            );
    
          }