gpt4 book ai didi

android - 从文件 :///data/user/0/. 读取 ionic 4 android 中的本地文件内容 (json) .. 路径不工作

转载 作者:行者123 更新时间:2023-11-30 05:00:54 24 4
gpt4 key购买 nike

我在使用 ionic-framework 在 android 上获取本地文件数据时遇到了一个非常奇怪的问题。

情况:我有一个先前在路径 file:///data/user/0/app-id/files/my-folder/xyz.json

上创建的文件

我可以在“adb shell”中找到文件,我也用cat检查了里面的数据,一切都很好。通过 Android-Studio 文件浏览器,我还检查了文件。

使用 native 文件插件 '@ionic-native/file/ngx' 我也可以确认文件在那里。

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, message: "ENCODING_ERR"}

我还尝试使用 native http 插件获取数据,使用 fetch 和普通的 http angular 请求...

一切都会导致 cors、404 等

总而言之,我如何从指向 file:///data/user/0/app-id/files/... 的本地路径获取 json-data/p>

ionic 信息:

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

最佳答案

我在 ionic Chooser 中遇到了相同\类似的问题,从谷歌驱动器中选择一个文件。您只需要将 native 路径拆分为路径和文件名即可。无论 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);
});

关于android - 从文件 :///data/user/0/. 读取 ionic 4 android 中的本地文件内容 (json) .. 路径不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58260967/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com