I'm trying to read a txt file located in the "sdcard" directory of my android phone. but the "onloadend" event of the "FileReader" never fires, in fact none of the events seem to work. I'm building the app with "phonegap-build" and I'm importing the cordova-plugin-file plugin by adding to the "config.xml" file the line:
<gap:plugin name="cordova-plugin-file" source="npm" />
The code is the following (the variables and functions are in portuguese, sorry):
// Importar dados
$("#ImportarDados").click(function() {
// obter sistema de ficheiros (diretoria principal)
window.resolveLocalFileSystemURL(
cordova.file.externalRootDirectory,
obtiveSistFich,
erro
);
// obter ficheiro
function obtiveSistFich(diretoria) {
console.log("obtive diretoria principal", diretoria);
var ficheiro_entrada = localStorage.getItem("dir_importacao")
diretoria.getFile(ficheiro_entrada, {create:true}, obtiveFicheiro, erro);
}
// ler ficheiro
function obtiveFicheiro(ficheiro) {
console.log("obtive o ficheiro", ficheiro);
ficheiro.file(ficheiroLido(ficheiro), erro);
}
// imprimir ficheiro
function ficheiroLido(ficheiro) {
console.log(leitor);
// TILL HERE SEEMS TO WORKS FINE!!
var leitor = new FileReader();
leitor.onloadend = function(evt) {
// IT NEVER WORKS!!!!
alert("sucesso de leitura!");
alert(evt.target.result);
};
leitor.readAsText(ficheiro);
}
// lidar com possíveis erros (debug)
function erro(erro) {
console.log(erro.code);
}
}
Just found out the problem. It appears that phonegap-build doesn't support the most recent version of the cordova-plugin-file.
The most recent version released (on npm) is 4.1.0 and on phonegap-build is 3.0.0.
Basically just need to change the line, in config.xml:
<gap:plugin name="cordova-plugin-file" source="npm" />
To:
<gap:plugin name="cordova-plugin-file" version="3.0.0" source="npm" />