从android读取ionic4上的本地文件内容(json)file:///data/user/0/... 路径不工作
我有一个非常奇怪的问题,就是在安卓系统上用离子框架获取本地文件数据 情况: 我有一个先前在路径上创建的文件file:///data/user/0/app-id/files/my-folder/xyz.json 我可以在“adb外壳”中找到文件,我还用cat检查了里面的数据,一切正常。通过Android Studio文件浏览器,我也检查了文件 使用本机文件插件“@ionic native/file/ngx”我还可以确认文件是否存在从android读取ionic4上的本地文件内容(json)file:///data/user/0/... 路径不工作,android,cordova,ionic-framework,cordova-plugins,ionic4,Android,Cordova,Ionic Framework,Cordova Plugins,Ionic4,我有一个非常奇怪的问题,就是在安卓系统上用离子框架获取本地文件数据 情况: 我有一个先前在路径上创建的文件file:///data/user/0/app-id/files/my-folder/xyz.json 我可以在“adb外壳”中找到文件,我还用cat检查了里面的数据,一切正常。通过Android Studio文件浏览器,我也检查了文件 使用本机文件插件“@ionic native/file/ngx”我还可以确认文件是否存在 const jsonFile = "pathToFile"; th
const jsonFile = "pathToFile";
this.file.resolveLocalFilesystemUrl(jsonFile).then(fileEntry => {
console.log(fileEntry.nativeURL);
}).catch(err => {
console.log(err);
});
结果:
filesystem: FileSystem {name: "files", root: DirectoryEntry}
isDirectory: false
isFile: true
它还可以正确解析其他内容,例如名称、完整路径、nativeURL
现在问题开始了,因为我找不到获取数据的方法
this.file.readAsText('', jsonFile).then(fileData => {...
this.file.readAsDataURL('', jsonFile).then(fileData => {...
this.file.readAsBinaryString('', jsonFile).then(fileData => {...
this.file.readAsArrayBuffer('', jsonFile).then(fileData => {...
所有这些都会导致{code:5,消息:“ENCODING_ERR”}
我还尝试使用本机http插件、fetch和普通http请求获取数据
所有的结果都是cors、404等
总之,如何从指向的本地路径获取json数据file:///data/user/0/app-id/files/...
爱奥尼亚信息:
Ionic:
Ionic CLI : 5.4.2 (xxxx/.npm-packages/lib/node_modules/ionic)
Ionic Framework : @ionic/angular 4.10.0
@angular-devkit/build-angular : 0.801.3
@angular-devkit/schematics : 8.1.3
@angular/cli : 8.1.3
@ionic/angular-toolkit : 2.0.0
Cordova:
Cordova CLI : 9.0.0 (cordova-lib@9.0.1)
Cordova Platforms : android 8.1.0
Cordova Plugins : cordova-plugin-ionic-keyboard 2.2.0, cordova-plugin-ionic-webview 4.1.2, (and 7 other plugins)
Utility:
cordova-res : 0.7.0
native-run : 0.2.8
System:
Android SDK Tools : 26.1.1 (xxxx/Library/Android/sdk)
ios-deploy : 1.9.4
NodeJS : v12.10.0 (/usr/local/Cellar/node/12.10.0/bin/node)
npm : 6.11.3
OS : macOS Mojave
Xcode : Xcode 11.0 Build version 11A420a
无效文件名?
根据,可能是您的文件名包含无效字符,例如:
,该字符在Windows上无效,因此保留
删除和添加平台?
似乎有人通过移除和添加平台解决了这个问题:
const displayImage = this.webview.convertFileSrc(storedPhoto);
我在ionic Chooser上也有类似的问题,从google drive中选择一个文件。您只需将本机路径拆分为路径和文件名。无论google驱动器上有多少子目录,路径都是相同的。仅在Android上测试。 如果有帮助的话,我的解决方案就在这里
readFileFromChooser() {
this.chooser.getFile('application/json')
.then((chooserResult: ChooserResult) => {
this.filePath.resolveNativePath(chooserResult.uri).then(nativePath => {
const lastSeparator = nativePath.lastIndexOf('/');
const file = nativePath.substr(lastSeparator + 1);
const path = nativePath.substr(0, lastSeparator);
this.file.readAsText(path, file)
.then(value => {
console.log('This is the file contents: ' + value);
}).catch(error => {
console.error(error);
});
}).catch(error => {
console.error(error);
});
}).catch(error => {
console.error(error);
});
}