Native File and ENCODING_ERR code 5


#1

Hello, I’m new in Ionic2, apps and even software development…

I had problems with Native File, trying to read an on-device file :
each time I tried to reach an existing file on the device, with the right path and the right filename as a string ( after testing if the directory exists and receiving a ‘true’ answer ) I always got an error : “code”:5,“message”:“ENCODING_ERR”.
(either if I try to test file existence with File.checkFile(Path, fileName ) or if I try to read the file using File.readAsText(Path,fileName) => same error message code 5 ENCODING_ERR)

Curiously, command " FileOpener.open(myFile, ‘text/plain’) " succeeded to open the file on device, proving that the file exists and is reachable via another Ionic command !

I finaly found a workaround using FilePath.resolveNativePath(myFile) and using the filePath returned by this promise.

Don’t know if this is the right way to access to an on-device file ; at least it works and, may be, this workaround can be usefull for other developers fighting with File native in Ionic2 !

For example this piece of code below works to demonstrate access to a on-device file :

var fileName = 'myFile.txt';
var fullPath = this.file.externalRootDirectory + 'Android/data/';

this.filePath.resolveNativePath(fullPath)
  .then(
    (filePath) => {
      this.file.readAsText(filePath, fileName).then(
        (res) => {console.log ('reading file = ' + filePath + fileName + ' ; rep = ' + res)},
        (err)  => {console.log ('reading file = ' + filePath + fileName + ' ; error = ' + JSON.stringify(err))}
      );
    })
  .catch(err => console.log('in resolveNativePath, '+  err));

#2

is https://ionicframework.com/docs/native/file/?


#3

Yes, it is.
Nothing in the doc ( https://ionicframework.com/docs/native/file/ ) says you should first resolveNativePath before accessing your file, but it seems to be the right way to access to an on-device file ; at least it works for me !