1

I have a really strange problem with getting local file data on android with ionic-framework.

Situation: I have a file that was earlier created on the path file:///data/user/0/app-id/files/my-folder/xyz.json

I can find the file inside the "adb shell", I also checked the data inside with cat and everything is fine. Over the Android-Studio File-Browser I also checked the file.

With the native file plugin '@ionic-native/file/ngx' I can also confirm, that the file is there.

const jsonFile = "pathToFile";
this.file.resolveLocalFilesystemUrl(jsonFile).then(fileEntry => {
      console.log(fileEntry.nativeURL);
    }).catch(err => {
      console.log(err);
    });

Result:

filesystem: FileSystem {name: "files", root: DirectoryEntry}
isDirectory: false
isFile: true

It also resolves the other stuff correct eg. name, fullPath, nativeURL

Now the problem starts, because I can't finde a way to get the data.

this.file.readAsText('', jsonFile).then(fileData => {...
this.file.readAsDataURL('', jsonFile).then(fileData => {...
this.file.readAsBinaryString('', jsonFile).then(fileData => {...
this.file.readAsArrayBuffer('', jsonFile).then(fileData => {...

All of them result in a {code: 5, message: "ENCODING_ERR"}

I also tried to get the data with the native http-plugin, with fetch and a normal http angular request...

Everything results in a cors, 404 etc.

So all in all, how can I get json-data from a local path that points to file:///data/user/0/app-id/files/...

ionic info:

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
Sylressa
  • 11
  • 1
  • 2

2 Answers2

1

I had the same\similar problem with ionic Chooser, choosing a file from google drive. you just have to split the native path into path and filename. The path is the same regardless of any sub directories on the google drive. Tested on Android only. My solution is here, if it helps..

  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);
      });

}

Phil
  • 377
  • 1
  • 2
  • 14
0

Invalid Filename?

According to this post, it might be if your filename has an invalid character, eg a : which is not valid on Windows and therefore reserved.

Remove and add platform?

It seems somebody solved this by removing and adding the platform:

Convert file:/// to http url?

Initially I thought this was the issue and started writing it, but I have less confidence in this one now. Leaving it in case it does help:

If you check out this blog post it talks about file urls:

const displayImage = this.webview.convertFileSrc(storedPhoto);
rtpHarry
  • 13,019
  • 4
  • 43
  • 64