How to Open PDF/File in Ionic on Android

How to Open PDF/File in Ionic on Android?

If you recently tried to open a file in Ionic from the application dir using `fileOpener2` plugin you may have encountered an error `File Not Found` which could indicate permission error.

It turns out you can’t open a file from `applicationDirectory` directly, you have to copy it into another directory:

if (this.platform.is('android')) {
  const self = this;
  const targetFile = self.file.dataDirectory + '/' + yourFileName;
		
  self.file.copyFile(self.file.applicationDirectory + 'www/assets/', yourFileName, 
                     self.file.dataDirectory, yourFileName).
           then(function (res) {
	     self.fileOpener.open(targetFile, 'application/pdf');
	});
}

Install the Cordova Plugin here:

https://github.com/pwlin/cordova-plugin-file-opener2
https://ionicframework.com/docs/native/file-opener