问题
I've got a really nasty problem here. Every time, I try to use the file and file-transfer plugins in my project, I get the Uncaught TypeError: Cannot read property 'dataDirectory' of undefined error while trying to get the data directory with store = cordova.file.dataDirectory;
. Now, I've been trying to solve my problem for hours, but I couldn't find any help. I'm also using the Barcode Scanner plugin in this project an it's working like a charm. I am using PhoneGap 3.6.3 and jQuery Mobile 1.4.4. Since i'm new to PhoneGap, it's likely that I'm missing something important...
I've included the plugins in my config.xml 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 Build log for Windows Phone is confirming that the plugins are being added. But it appears that they're not being added to the Android build, since I can't find any reference of them in the build log.
Windows Phone Log:
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 a part of my index.js, excluding 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.
回答1:
I've always thought that you need to first call
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, onFileSystemSuccess, onError);
for cordova.file stuff to get defined and to 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);
来源:https://stackoverflow.com/questions/27572160/phonegap-build-plugins-not-working-getting-undefined-errors-on-android