简体   繁体   English

无法使用cordova-plugin-file插件从android目录中读取文件

[英]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.

相关问题 cordova-plugin-file 无法将文件从库复制到 cordova.file.dataDirectory - cordova-plugin-file can't copy file from gallery to cordova.file.dataDirectory 如何使用cordova-plugin-file在cordova应用程序之间共享数据? (Android,IOS) - How to share data between cordova apps using cordova-plugin-file? (Android, IOS) 我无法在cordova-plugin-file中运行代码 - I ain't able to run code in cordova-plugin-file cordova-plugin-file播放来自extSD的视频文件 - cordova-plugin-file to play video file from extSD Android上的Cordova插件文件Encoding_Err - Cordova-plugin-file Encoding_Err on Android 读取大于20MB的文件时,Crosswalk使用cordova-plugin-file崩溃 - Crosswalk crashes with cordova-plugin-file when read file larger than 20MB Android、Cordova/PhoneGap、Cordova-Plugin-File 上的 SMS/MMS 数据位置 - SMS/MMS data location on Android, Cordova/PhoneGap, Cordova-Plugin-File 在Windows 10中使用cordova-plugin-file复制SQLite数据库cordova-sqlite-storage - Using cordova-plugin-file to copy SQLite database cordova-sqlite-storage in Windows 10 Cordova文件插件 - 从www文件夹中读取? - Cordova File Plugin - Read from www folder? 构建错误cordova应用程序,“错误:404未找到:cordova-plugin-file” - Build error cordova application, “Error: 404 Not Found: cordova-plugin-file”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM