简体   繁体   中英

Android 10 @ionic-native/file-transfer /file-opener not working

@ionic-native/file-transfer /file-opener not working I'm getting "open failed: EACCES (permission denied) error while trying to download a pdf and open in my ionic-angular project. This is the header files and function to open the attachment. This is working fine in android version 9 but not in android 10. What is the reason for this issue?

Header files

import { FileTransfer, FileTransferObject } from '@ionic-native/file-transfer';
import { File } from '@ionic-native/file';
import { FileOpener } from '@ionic-native/file-opener';
import { PhotoViewer } from '@ionic-native/photo-viewer';

Function call

constructor(public navCtrl: NavController, public navParams: NavParams,
...
private fileTransfer: FileTransfer,
private platform: Platform,
private file: File,
private fileOpener: FileOpener,
private photo: PhotoViewer,
...
) {...}

openAttachment(attachment) {
this.notification.getNotificationCount(this.userID).subscribe(res => this.setNotificationsCountAtStart(res));
this.loader.displayLoader();
const transfer: FileTransferObject = this.fileTransfer.create();
var filename = attachment.substring(attachment.lastIndexOf('/') + 1);
var filePath;
if (this.platform.is('ios')) {
  filePath = this.file.documentsDirectory + filename;
} else if (this.platform.is('android')) {
  filePath = this.file.externalRootDirectory + 'Download/' + filename;
}
if (attachment.indexOf('.pdf') > -1) {
  transfer.download(this.baseurl + attachment, filePath, true).then((entry) => {
    let url = entry.toURL();
    this.fileOpener.open(url, 'application/pdf')
      .then(() => {
        console.log('File is opened');
        this.loader.hideLoader();
      })
      .catch(e => console.log('Error opening file', JSON.stringify(e)))
  }, (error) => {
    // handle error
    let toast = this.toast.create({
      message: JSON.stringify(error),
      duration: 3000,
      position: 'bottom'
    });
    toast.present();
  });
} else if (attachment.indexOf('.png') > -1) {
  transfer.download(this.baseurl + attachment, filePath, true).then(entry => {
    let url = entry.toURL();
    this.loader.hideLoader();
    this.photo.show(url, filename, {});
  })
} else if (attachment.indexOf('.jpg') > -1) {
  transfer.download(this.baseurl + attachment, filePath, true).then(entry => {
    let url = entry.toURL();
    this.loader.hideLoader();
    this.photo.show(url, filename, {});
  })
} else if (attachment.indexOf('.jpeg') > -1) {
  transfer.download(this.baseurl + attachment, filePath, true).then(entry => {
    let url = entry.toURL();
    this.loader.hideLoader();
    this.photo.show(url, filename, {});
  })
} else {
  this.loader.hideLoader();
}
}

I've changed the AndroidTargetSDK version to 28 in AndroidManifest file. Now its working. I'm running the app from AndroidStudio. So changing the value to 28 makes it working. Hope this works.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM