使用 Cordova 文件插件读取文件

Reading file using Cordova File Plugin

我正在使用 cordova 文件插件 - cordova file plugin

我实际上是在从文本文件中读取文件。下面是我的代码

document.addEventListener("deviceready", function () {  
    window.resolveLocalFileSystemURL(cordova.file.applicationDirectory + "sameple.txt", gotFile, fail);
}, true);

function gotFile(file) {           
    file.file(function (file) {                
        var reader = new FileReader();
        reader.onloadend = function (evt) {          
            console.log(this.result);
        }
        reader.readAsText(file);                
    }, fail());            
}

function fail(e) {
    console.info("FileSystem Error : " + e);
}

所以每当我 运行 这段代码时,我都会收到以下错误

deviceready has not fired after 5 seconds.
Channel not fired: onPluginsReady
Channel not fired: onCordovaReady

Could not get Cordova FileSystem: Error: deviceready has not fired after 5 seconds.
   "Could not get Cordova FileSystem:"
   {
      [functions]: ,
      __proto__: { },
      description: "deviceready has not fired after 5 seconds.",
      message: "deviceready has not fired after 5 seconds.",
      name: "Error"
   }

{"data":"data"}

deviceready error 之后我可以得到准确的数据。我该如何解决这个错误?我必须等待设备就绪执行完成吗?

发生这种情况的原因有很多,请参阅 here 更多详细信息。

您应该先尝试 @bayanAbuawad suggestion 大多数情况下都有效:

  1. 使用cordova platform add 添加平台ios android

  2. 使用 cordova platform remove 删除它们 ios android

  3. 重新添加。

超级奇怪,但这与平台文件夹中的错误 android.json 和 ios.json 有关。