首页 > 解决方案 > 使APP android读取文件的内容 - IONIC

问题描述

我的问题是,如果可以从一个.properties.txt文件中获取一个变量。我需要它来保存endPoint服务器 IP 地址。这样,每次更改IP地址时,您只需更改文件上的IP,而不是再次编译代码并构建apk,然后在设备上重新安装。欢迎任何提示。

更新 我一直在尝试使用这个插件:File,但没​​有成功。我想使用readAsText(path,file),但我得到的只是未定义

import { File } from '@ionic-native/file/ngx';

constructor(
    ....
    private file : File,
    ....
  ) { }

async test(){
this.promise = this.file.readAsText('file:///data/','ipAddress.txt'); 
await this.promise.then(value => {
console.log(value)
}).catch(error=> console.log("nao existe"))
}

我收到以下错误:

ERROR Error: Uncaught (in promise): FileError: {"code":1,"message":"NOT_FOUND_ERR"}

更新 问题已解决。我试图访问内部存储,而文档在外部存储上。确保您使用 URI,android 上的路径与计算机不同。您需要 URI。

file:///storage/emulated/0/Android/data/

当设备准备就绪时,我立即调用了该方法app.component.ts

  ngOnInit() {  
  ..... 
  this.initializeApp();
  }

initializeApp() {
this.platform.ready().then(() => {  
this.api.getURL();
.....
});
}

希望我能帮助下一个解决我遇到的同样问题。

标签: androidangularionic-frameworkapk

解决方案


推荐阅读