cordova-plugin-file-opener2: Opening local file (pdf) : “not found”.

Hello,

Using io.github.pwlin.cordova.plugins.fileopener2, I can’t open my local PDF file in Android.

Locally, I have a “/myapp/www/pdf/” folder with files (1.pdf,… to 6.pdf).

Here my PhoneGap config.xml

<widget xmlns="http://www.w3.org/ns/widgets" xmlns:gap="http://phonegap.com/ns/1.0" id="app.example.com" version="1.0.0">
  <name><![CDATA[Example]]></name>
  <description>...</description>
  <author href="http://www.example.com" email="hello@example.com">Example</author>
  <content src="index.html"/>
  <preference name="permissions" value="none"/>
  <preference name="orientation" value="default"/>
  <preference name="target-device" value="universal"/>
  <preference name="fullscreen" value="true"/>
  <preference name="webviewbounce" value="false"/>
  <preference name="prerendered-icon" value="true"/>
  <preference name="stay-in-webview" value="false"/>
  <preference name="ios-statusbarstyle" value="black-opaque"/>
  <preference name="detect-data-types" value="true"/>
  <preference name="exit-on-suspend" value="false"/>
  <preference name="show-splash-screen-spinner" value="true"/>
  <preference name="auto-hide-splash-screen" value="true"/>
  <preference name="disable-cursor" value="false"/>
  <preference name="android-minSdkVersion" value="7"/>
  <preference name="android-installLocation" value="auto"/>
  <preference name="useBrowserHistory" value="true" />
  <gap:plugin name="org.apache.cordova.console"/>
  <gap:plugin name="org.apache.cordova.device"/>
  <gap:plugin name="org.apache.cordova.dialogs"/>
  <gap:plugin name="org.apache.cordova.file"/>
  <gap:plugin name="org.apache.cordova.file-transfer"/>
  <gap:plugin name="org.apache.cordova.inappbrowser"/>
  <gap:plugin name="org.apache.cordova.network-information"/>
  <gap:plugin name="io.github.pwlin.cordova.plugins.fileopener2" version="1.0.11" />
  <icon src="icon.png"/>
  <icon src="res/icon/android/icon-36-ldpi.png" gap:platform="android" gap:qualifier="ldpi"/>
  <icon src="res/icon/android/icon-48-mdpi.png" gap:platform="android" gap:qualifier="mdpi"/>
  <icon src="res/icon/android/icon-72-hdpi.png" gap:platform="android" gap:qualifier="hdpi"/>
  <icon src="res/icon/android/icon-96-xhdpi.png" gap:platform="android" gap:qualifier="xhdpi"/>
  <icon src="res/icon/ios/icon-57.png" gap:platform="ios" width="57" height="57"/>
  <icon src="res/icon/ios/icon-72.png" gap:platform="ios" width="72" height="72"/>
  <icon src="res/icon/ios/icon-57-2x.png" gap:platform="ios" width="114" height="114"/>
  <icon src="res/icon/ios/icon-72-2x.png" gap:platform="ios" width="144" height="144"/>
  <gap:splash src="res/screen/android/screen-ldpi-portrait.png" gap:platform="android" gap:qualifier="port-ldpi"/>
  <gap:splash src="res/screen/android/screen-mdpi-portrait.png" gap:platform="android" gap:qualifier="port-mdpi"/>
  <gap:splash src="res/screen/android/screen-hdpi-portrait.png" gap:platform="android" gap:qualifier="port-hdpi"/>
  <gap:splash src="res/screen/android/screen-xhdpi-portrait.png" gap:platform="android" gap:qualifier="port-xhdpi"/>
  <gap:splash src="res/screen/ios/screen-iphone-portrait.png" gap:platform="ios" width="320" height="480"/>
  <gap:splash src="res/screen/ios/screen-iphone-portrait-2x.png" gap:platform="ios" width="640" height="960"/>
  <gap:splash src="res/screen/ios/screen-iphone-portrait-568h-2x.png" gap:platform="ios" width="640" height="1136"/>
  <gap:splash src="res/screen/ios/screen-ipad-portrait.png" gap:platform="ios" width="768" height="1024"/>
  <gap:splash src="res/screen/ios/screen-ipad-landscape.png" gap:platform="ios" width="1024" height="768"/>
  <access origin="*"/>
  <plugin name="cordova-plugin-whitelist" version="1"/>
  <allow-intent href="http://*/*"/>
  <allow-intent href="https://*/*"/>
  <allow-intent href="tel:*"/>
  <allow-intent href="sms:*"/>
  <allow-intent href="mailto:*"/>
  <allow-intent href="geo:*"/>
  <platform name="android">
    <allow-intent href="market:*"/>
  </platform>
  <platform name="ios">
    <allow-intent href="itms:*"/>
    <allow-intent href="itms-apps:*"/>
  </platform>
  <feature name="http://api.phonegap.com/1.0/file"/>
</widget>

I already tried many things like that in my html files :

<!-- openPDF is a personnal function... see next block -->
<a onClick="openPDF('pdf/1.pdf');">Open PDF</a>

My functions (js files) :

var fs;

function fsSuccess(fileSystem)
{
    fs = fileSystem;
}

function fsFail(event)
{
    console.log(event.target.error.code);
}

function openPDF(file) {

    window.requestFileSystem  = window.requestFileSystem || window.webkitRequestFileSystem;
    window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, fsSuccess, fsFail);     

    file = fs.root.toURL() + file;

    cordova.plugins.fileOpener2.open(
    file, // You can also use a Cordova-style file uri: cdvfile://localhost/persistent/Download/starwars.pdf
    'application/pdf', 
    { 
        error : function(e) { 
            console.log('Error status: ' + e.status + ' - Error message: ' + e.message);
            $('#errorpdf').html('Error status: ' + e.status + ' - Error message: ' + e.message+' - File : '+file);
        },
        success : function () {
            console.log('file opened successfully');          
            $('#errorpdf').html('file opened successfully');

        }
    }
);
}

All the time, Cordova returns me : “Error status: 9 - Error message: File not found”

Where am I wrong ?

EDIT : my folders /storage/emulated/0/Android/data/com.example.app/files /storage/emulated/0/Android/data/com.example.app/cache obviously is empty and I don’t know how to move local .pdf file to this folder.

About this issue

  • Original URL
  • State: closed
  • Created 9 years ago
  • Comments: 23 (1 by maintainers)

Most upvoted comments

I had the same problem trying to open a PDF located in the www/ folder. So to open it in android: I copied it to the external data directory and then I opened the new file:

(uri is /www/path/to/file.pdf)

window.resolveLocalFileSystemURL(cordova.file.applicationDirectory +  uri, function(fileEntry) {

    window.resolveLocalFileSystemURL(cordova.file.externalDataDirectory, function(dirEntry) {

        fileEntry.copyTo(dirEntry, 'file.pdf', function(newFileEntry) {

            cordova.plugins.fileOpener2.open(newFileEntry.nativeURL,'application/pdf');
        });
    });
});

Thanks to @japostigo-atsistemas solution!

I tweaked it a bit, here’s what I recommend:

  1. use window.open instead of fileOpener2.open
  1. use uri.split('/').pop() to keep the original filename when copying
window.resolveLocalFileSystemURL(cordova.file.applicationDirectory +  uri, function(fileEntry) {

    window.resolveLocalFileSystemURL(cordova.file.externalDataDirectory, function(dirEntry) {

        fileEntry.copyTo(dirEntry, uri.split('/').pop(), function(newFileEntry) {

            window.open(newFileEntry.nativeURL, '_system');
        });
    });
});

@Balaji-yogy I had the same issue. I changed from window.TEMPORARY to LocalFileSystem.PERSISTENT and it worked.