无法使用FileOpener2打开文件,但无法在Android中收到错误
我正在尝试使用FileOpener2(通过ng-cordova)使用以下代码打开PDF文件:
I am attempting to open a PDF file with FileOpener2 (through ng-cordova) with the following code:
$cordovaFile.checkFile(cordova.file.dataDirectory, attachmentPath)
.then((fileEntry) => {
// success
fileEntry.getMetadata((metadata) => {
// metadata.size is in bytes
var megabyteSize = metadata.size / 1048576;
if (megabyteSize > 5) {
var path = cordova.file.dataDirectory + attachmentPath;
console.log(path); // prints: file:///data/data/com.ionicframework.enhatch146189/files/attachments/CS-353ES_CS-420ES_Eng.pdf which is correct
$cordovaFileOpener2.open(path, 'application/pdf').then(() => {
console.log("Opened!") // prints
}, (error) => {
console.log(error);
usePDFJs(); // tries to render PDF in app with PDFJs
});
} else {
usePDFJs();
}
})
}, function (error) {
// error
console.error(error);
});
让我感到困惑的是:它提示我在Adobe Reader中打开此文件?并列出其他PDF查看器,控制台打印打开!
What happens confuses me: it prompts me with an "open this file in Adobe Reader?" and lists the other PDF viewers, and the console prints "Opened!"
然而,无论我打开任何pdf,我都会遇到某种错误,例如无法打开此PDF文件。
However, no matter what I open ANY pdf in, I get some sort of error such as "cannot open this PDF file".
有人能看到这段代码有问题吗?
Can anyone see something wrong with this code?
显然,如果您在Android上使用 cordova.file.dataDirectory
,则无法在其他应用程序中打开这些文件或将其附加到电子邮件中。愚蠢的错误 - 编码太快,在文档上读得太少。使用 cordova.file.externalApplicationStorageDirectory
解决了这个问题。
Apparently, if you use cordova.file.dataDirectory
on android you can't open those files in other applications or attach them to emails. Silly mistake -- coded too fast and read too little on the documentation. Using cordova.file.externalApplicationStorageDirectory
solved the issue.