无法从android目录使用cordova-plugin-file插件读取文件 [英] Can't read a file from android directory using cordova-plugin-file plugin

查看:1834
本文介绍了无法从android目录使用cordova-plugin-file插件读取文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试读取位于我的android手机的sdcard目录中的txt文件。但是FileReader的onloadend事件从不触发,事实上没有任何事件似乎工作。我正在使用phonegap-build创建应用,并且正在导入 cordova-plugin-file 插件,通过向config.xml文件中添加行:

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);
    }
  }


推荐答案

发现问题。看来 phonegap-build 不支持cordova-plugin-file的最新版本。

Just found out the problem. It appears that phonegap-build doesn't support the most recent version of the cordova-plugin-file.

发布的最新版本(在npm上)是 4.1.0 ,phonegap-build上的是 3.0.0

The most recent version released (on npm) is 4.1.0 and on phonegap-build is 3.0.0.

基本上只需要更改config.xml中的行:

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" />

这篇关于无法从android目录使用cordova-plugin-file插件读取文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆