简体   繁体   English

使用Cordova在Android Studio中直接从SD卡读取文本文件

[英]Read text file directly from sdcard in Android studio using cordova

I am working on a project which has a task to read a text file from the sdcard. 我正在做一个项目,该项目的任务是从sdcard读取文本文件。 The file path is given on the code. 文件路径在代码中给出。 I tried using this code but no output. 我尝试使用此代码,但没有输出。

 var path = "Download/myfile.txt";

window.resolveLocalFileSystemURL(path, success, fail);

function fail(e) {
      console.error(e);
}

function success(fileEntry) {
   fileEntry.file(function(file) {
           var reader = new FileReader();
           reader.onloadend = function(e) {
           var content = this.result;
           console.log(content);
       };
       reader.readAsText(file); // or the way you want to read it
   });
}

replace 更换

 var path = "Download/myfile.txt";

with

var path = cordova.file.externalRootDirectory+"/Download/myfile.txt;

and make sure <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> is there in Android Manifest file. 并确保Android清单文件中有<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM