Needs to enable storage permission manually in android device while downloading file ( IONIC 2 cordova-file-plugin)

I am using cordova file plugin’s download method to download file on android device.
I get file download successful message only if I enable the storage pemission by going to Settings>> Apps>> Myapp >> permission > storage

here is code I used
"

downloadImage(image) {

this.platform.ready().then(() => {

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

  const imageLocation = `${cordova.file.applicationDirectory}www/assets/img/${image}`;

  fileTransfer.download(imageLocation, this.storageDirectory + image).then((entry) => {

    const alertSuccess = this.alertCtrl.create({
      title: `Download Succeeded!`,
      subTitle: `${image} was successfully downloaded to: ${entry.toURL()}`,
      buttons: ['Ok']
    });

    alertSuccess.present();

  }, (error) => {

    const alertFailure = this.alertCtrl.create({
      title: `Download Failed!`,
      subTitle: `${image} was not successfully downloaded. Error code: ${error.code}`,
      buttons: ['Ok']
    });

    alertFailure.present();

  });

});

}

"

Use Ionic Diagnostic
https://ionicframework.com/docs/native/diagnostic/

2 Likes

Thanks Marcuslll. As I am new to IONIC, can you please suggest me to which function should I use in my case?

Yeah sure

this.diagnostic.requestExternalStorageAuthorization().then(()=>{
//User gave permission 
}).catch(error=>{
//Handle error
});

What this will do it will check if the app already have the permission if it does it will do nothing
if it doesn’t have permission it will ask the user to give permission

8 Likes

MarcusIII Thanks a lot. It worked :slight_smile:

1 Like

where put this function please tell me

or else you can try ANDROIDPERMISSIONS plugin
I can’t able to import diagnostic so i’ve used AndroidPermissons plugin

checkPermissions(){
    this.androidPermissions.requestPermissions(
        [
            this.androidPermissions.PERMISSION.READ_EXTERNAL_STORAGE, 
            this.androidPermissions.PERMISSION.WRITE_EXTERNAL_STORAGE
        ]
    );
}

Can I know if user allow or deny the permission?

requestPermissions seems to return a Promise that resolves with that information.