我正在尝试使用 FileOpener2(通过 ng-cordova)使用以下代码打开 PDF 文件:
$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 查看器,控制台打印“已打开!”
但是,无论我用什么格式打开任何 pdf,我都会收到某种错误,例如“无法打开此 PDF 文件”。
有人能看出这段代码有什么问题吗?
最佳答案
显然,如果您在 Android 上使用 cordova.file.dataDirectory
,您将无法在其他应用程序中打开这些文件或将它们附加到电子邮件中。愚蠢的错误——编码太快并且阅读的文档太少。使用cordova.file.externalApplicationStorageDirectory解决了这个问题。
关于android - 无法使用 FileOpener2 打开文件,但在 Android 中未出现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31148545/