Can't read a file from android directory using cordova-plugin-file plugin

独自空忆成欢 提交于 2019-12-12 03:25:41

问题


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

回答1:


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


来源:https://stackoverflow.com/questions/35303863/cant-read-a-file-from-android-directory-using-cordova-plugin-file-plugin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!