PhoneGap Build: plugins not working (getting "undefined" errors) on Android
I have a really annoying problem here. Every time I try to use file and file plugins in my project, I get Uncaught TypeError: Can't read dataDirectory property from undefined when trying to get data directory from store = cordova.file.dataDirectory;
. Now I have been trying to solve my problem for hours, but I could not find any help. I am also using the Barcode Scanner plugin on this project and it works like a charm. I am using PhoneGap 3.6.3 and jQuery Mobile 1.4.4. Since I'm new to PhoneGap, chances are I'm missing something important ...
I have included plugins in my config.xml file like this:
<gap:plugin name="org.apache.cordova.file" version="1.3.1" />
<gap:plugin name="org.apache.cordova.file-transfer" version="0.4.6" />
Maybe this could be a hint: the PhoneGap for Windows Phone build log confirms that plugins are being added. But it looks like they are not being added to the Android build as I cannot find them in the build log.
Windows Phone Journal:
Adding www \ plugins \ org.apache.cordova.file-transfer \ www \ FileTransfer.js
Adding www \ plugins \ org.apache.cordova.file-transfer \ www \ FileTransferError.js
Adding www \ plugins \ org.apache.cordova.file \ www \ File.js
This is part of my index.js, except for the barcode scanner functionality.
var app = {
// Application Constructor
initialize: function() {
this.bindEvents();
},
// Bind Event Listeners
bindEvents: function() {
document.addEventListener('deviceready', this.onDeviceReady, false);
document.getElementById('download').addEventListener('click', this.downloadFile, false);
document.getElementById('scan').addEventListener('click', this.scan, false);
document.getElementById('encode').addEventListener('click', this.encode, false);
},
onDeviceReady: function() {
app.receivedEvent('deviceready');
},
// Update DOM on a Received Event
receivedEvent: function(id) {
var parentElement = document.getElementById(id);
var listeningElement = parentElement.querySelector('.listening');
var receivedElement = parentElement.querySelector('.received');
listeningElement.setAttribute('style', 'display:none;');
receivedElement.setAttribute('style', 'display:block;');
console.log('Received Event: ' + id);
},
downloadFile: function(){
//The directory to store data
var store;
//Used for status updates
var $status;
//URL of our asset
var assetURL = "https://www.dropbox.com/s/d4s8mnkfwdqylns/test.txt?dl=0";
//File name of our important data file we didn't ship with the app
var fileName = "test.txt";
document.addEventListener("deviceready", init, false);
function init() {
$status = document.querySelector("#fileStatus");
$status.innerHTML = "Checking for file";
store = cordova.file.dataDirectory;
//Check for the file.
window.resolveLocalFileSystemURL(store + fileName, appStart, downloadAsset);
}
function downloadAsset() {
var fileTransfer = new FileTransfer();
console.log("About to start transfer");
fileTransfer.download(assetURL, store + fileName,
function(entry) {
console.log("Success!");
appStart();
},
function(err) {
console.log("Error");
console.dir(err);
});
}
function appStart() {
$status.innerHTML = "Datei aktuell";
}
},
// [...Functions for Barcode scanner...]
};
I've also included the phonegap.js file in my index.html:
<body>
...
<script type="text/javascript" charset="utf-8" src="phonegap.js"></script>
<script type="text/javascript" src="js/index.js"></script>
<script type="text/javascript">
app.initialize();
</script>
</body>
I really hope someone can help me solve my problem.
source to share
I always thought that you need to call first
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, onFileSystemSuccess, onError);
for the cordova.file to be defined and be able to call resolveLocalFileSystemURL.
So in your case try this modification
function onFileSystemSuccess() {
store = cordova.file.dataDirectory;
//Check for the file.
window.resolveLocalFileSystemURL(store + fileName, appStart, downloadAsset);
}
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, onFileSystemSuccess, onError);
source to share