如何从Ionic2和Android 5上的SDCARD读取文本文件



我正在尝试打开一个文本文件,并将内容放入sqlite db中。问题是我无法以任何方式阅读文件。以下是我设法实现的最接近的成功。执行后,它会产生fileError(2):" Security_err"。

openFile(): void {
    FileChooser.open()
    .then(uri => {
      console.log(uri);
      File.resolveLocalFilesystemUrl(uri)
      .then(entry=>{
        console.log(entry);
        let path = entry.nativeURL.substring(0, entry.nativeURL.lastIndexOf('/'));
        console.log(path);
        File.readAsText(path, entry.name)
        .then(content=>{
          console.log(content);
        })
      })
      .catch(e => console.log(e));
    });
  }

在电话上,我看到该应用程序在SD卡上具有RW访问权限,并且在platform/AndroidManifest.xml中我设置了:

  <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

我花了很多时间在互联网上挖掘并尝试不同的解决方案而没有成功。

编辑:

这是console.log(cordova.file);输出:

applicationDirectory: "file:///android_asset/"
applicationStorageDirectory: "file:///data/data/com.ionicframework.leki645195/"
cacheDirectory: "file:///data/data/com.ionicframework.leki645195/cache/"
dataDirectory: "file:///data/data/com.ionicframework.leki645195/files/"
documentsDirectory: null
externalApplicationStorageDirectory: "file:///storage/emulated/0/Android/data/com.ionicframework.leki645195/"
externalCacheDirectory: "file:///storage/emulated/0/Android/data/com.ionicframework.leki645195/cache/"
externalDataDirectory: "file:///storage/emulated/0/Android/data/com.ionicframework.leki645195/files/"
externalRootDirectory: "file:///storage/emulated/0/"
sharedDirectory: null
syncedDataDirectory: null
tempDirectory: null

尝试此代码,

openFile() {
  this.fileChooser.open()
  .then(uri => {
    this.file.resolveLocalFilesystemUrl(uri)
    .then(entry=>{
      let path = entry.nativeURL;
      //read file contents
      var rawFile = new XMLHttpRequest();
      rawFile.open("GET", path, false);
      rawFile.onreadystatechange = function ()
      {
          if(rawFile.readyState === 4)
          {
              if(rawFile.status === 200 || rawFile.status == 0)
              {
                var fileText = rawFile.responseText;
                alert(fileText); 
              }
          }
      }
      rawFile.send(null);
    })
    .catch(e => console.log(e));
  });
}

相关内容

  • 没有找到相关文章

最新更新