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


#1

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();

  });

});

}

"


#2

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


#3

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


#4

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


#5

MarcusIII Thanks a lot. It worked :slight_smile:


#6

where put this function please tell me