Phil Barresi
Phil Barresi

Reputation: 1355

Cannot open files with FileOpener2, but not getting an error in Android

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

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!"

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?

Upvotes: 2

Views: 1470

Answers (1)

Phil Barresi
Phil Barresi

Reputation: 1355

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.

Upvotes: 3

Related Questions