[英]Can't read a file from android directory using cordova-plugin-file plugin
I'm trying to read a txt file located in the "sdcard" directory of my android phone. 我正在尝试读取位于我的Android手机的“ sdcard”目录中的txt文件。 but the "onloadend" event of the "FileReader" never fires, in fact none of the events seem to work.
但是“ FileReader”的“ onloadend”事件从未触发,实际上,所有这些事件似乎均不起作用。 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:
我正在使用“ phonegap-build”构建应用程序,并通过在“ config.xml”文件中添加以下行来导入cordova-plugin-file插件:
<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.
似乎phonegap-build不支持cordova-plugin-file的最新版本。
The most recent version released (on npm) is 4.1.0 and on phonegap-build is 3.0.0 . (在npm上)发布的最新版本是4.1.0 ,在phonegap-build上是3.0.0 。
Basically just need to change the line, in config.xml: 基本上只需要在config.xml中更改这一行:
<gap:plugin name="cordova-plugin-file" source="npm" />
To: 至:
<gap:plugin name="cordova-plugin-file" version="3.0.0" source="npm" />
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.