cordova-plugin-file-opener2: Can't open file in cordova.file.cacheDirectory
I created a temp file “temp.pdf” under cordova.file.cacheDirectory and need to open it after it is created. I don’t know which kind of file path of cordova.file.cacheDirectory can be accepted by fileOpener2.open. Here is my code.
private openFile(fileName, contentType) {
var pathToFile = cordova.file.cacheDirectory + fileName;
window.resolveLocalFileSystemURI(pathToFile, (fileEntry: FileEntry) => {
var filePath = fileEntry.toURL().substring(8);
// var filePath = fileEntry.fullPath;
(<ICordovaPlugins>window.cordova.plugins).fileOpener2.open(filePath, contentType, {
error: (errorObj: any) => {
console.log("Cannot open file, failed to open the temporary file: " + errorObj.status + ': ' + errorObj.message);
},
success: () => {
console.log(fileEntry.fullPath);
}
})
},
() => {
console.log("Cannot open file, failed to get cache directory.");
}
);
}
To open a file under cordova.file.cacheDirectory, which path does fileOpener2.open expect?
(1) For fileEntry.toURL(), I got “file:///data/data/AppID*****/cache/temp.pdf” (2) For fileEntry.fullPath, I got “/temp.pdf” (3) For fileEntry.toInternalURL(), I got “cdvfile://localhost/cache/temp.pdf”
I have tired all of the paths above, none of them can open the temp file which was created successfully. Is cordova.file.cacheDirectory an internal location rather than an external location so that the file cannot be open from this location using fileOpener2?
About this issue
- Original URL
- State: closed
- Created 8 years ago
- Comments: 16 (7 by maintainers)
@pwlin Sorry I haven’t managed to try Cordova-Open but finally got your plugin working in my case.
The reason that your plugin didn’t work is because Android restricts the access of third party tools to the folder cordova.file.cacheDirectory although iOS does not have such a restriction.
So my solution is to store the temp file in cordova.file.externalDataDirectory on Android and cordova.file.cacheDirectory in iOS.
Another fix I need to do is to get the file entry’s path using toURL() rather than toInternalURL().
if (device.iOS == true) folder = cordova.file.cacheDirectory; if (device.android == true) folder = cordova.file.externalDataDirectory;
var pathToFile = folder + fileName; window.resolveLocalFileSystemURL(pathToFile, function (entry) { cordova.plugins.fileOpener2.open( entry.toURL(), ‘application/pdf’, { error: function (e) { console.log('Error status: ’ + e.status + ’ - Error message: ’ + e.message); }, success: function () { console.log(‘file opened successfully’); } } ); }, function (e) { console.log(‘File Not Found’); })
As we need to clean up the files due to privacy reason, we have another process to remove the temp file from the folder (extenalDataDirectory on Android and cacheDirectory on iOS) every 10 seconds.
The plugin works like a charm now.
resolveLocalFileSystemURI
is deprecated. UseresolveLocalFileSystemURL
Also, Every time you debug your app (at least on Android), your whole
/data/data/AppID*****/
gets emptied. So remember to download your file every time during debugging tocordova.file.cacheDirectory
directory.Can you check if the following example works for you? (I successfully tested it on Android 4.4): Try it just as it is, without converting it to ES6. And make sure to download the file to the cache directory every time during debugging before calling the code:
And if it really really doesn’t work for you, use the official, cross-platform
InAppBrowser
plugin which is listed at official plugins pagecordova plugin add cordova-plugin-inappbrowser
It will open the file in whatever default application available for that type of file,