Florian Mithieux
Florian Mithieux

Reputation: 503

Cordova FileOpener Android - can't find a local file location

Using Cordova FileOpener2, and many others plugins, like Cordova File, Cordova FileTransfer.. I can't found my local PDF location in Android.

This file is in the www/offline-files/ directory and when I opened it in iOS (with window.open(encodeURI('offline-files/myFile.pdf'), '_blank');) it works fine ! If I'm trying the same thing in Android, it doesn't work. For example, one of my many tries:

function getPath()
{
    // Get local path for Cordova
    var path = window.location.pathname;
    path = path.substr(path, path.length - 10);
    return 'file://' + path;
}

cordova.plugins.fileOpener2.open(
    getPath() + 'offline-files/myFile.pdf',
    'application/pdf',
    {
        error: function (e)
        {
            console.log('Error status: ' + e.status + ' - Error message: ' + e.message);
        },
        success: function ()
        {
            console.log('file opened successfully');
        }
    }
);

Cordova returns me the log: "Error status: 9 - Error message: File not found"

There's also a strange error: Cordova file plugin returns me an error file not found for the classic directory:

window.resolveLocalFileSystemURL(cordova.file.applicationDirectory, 
    function (dir)
    { 
        /** SOME CODE **/
    }
);

Upvotes: 3

Views: 1895

Answers (1)

pabloapa
pabloapa

Reputation: 165

What version of Cordova are you using? In any case, I wouldn't hardcode the filepath and get it from the LocalFileSystem object instead.

var fs;

function fsSuccess(fileSystem)
{
    fs = fileSystem;
}

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

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

Then access the root path via:

fs.root.toURL() + "yourfilename"

Hope this helps!

Upvotes: 2

Related Questions