Lars
Lars

Reputation: 2485

NOT_FOUND_ERR while trying to load Ionic file as blob from android device

I am getting a FileError {code: 1} while trying to load a file from my android device using cordova-plugin-file. My code:

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

private loadGpkgBlob(): Promise<any> {
  const gpkgPath = this.file.externalRootDirectory + "test_dir/test.gpkg";

  return new Promise((resolve, reject) => {
    return this.file.resolveLocalFilesystemUrl(gpkgPath).then((fileEntry: any) => {
      fileEntry.file(gpkgFile => {
        // file is present (see screenshot 1)
        console.log("gpkgFile: ", gpkgFile);

        const reader = new FileReader();

        reader.onloadend = function (e) {
          const blob = new Blob([reader.result], { type: 'application/geopackage+sqlite3' });
          // blob with size 4 (see screenshot 2)
          console.log("result: ", blob);
          resolve(blob);
        };

        reader.onerror = function (e) {
          // FileError {code: 1} -> NOT_FOUND_ERR (https://developer.mozilla.org/en-US/docs/Web/API/FileError)
          console.log("error: ", reader.error);
        };

        reader.readAsArrayBuffer(gpkgFile);
      });
    }).catch(e => e => console.log(e));
  });
}

Screenshot 1 - file on the device:

enter image description here

Screenshot 2 - gpkg-blob:

enter image description here

The file is obviously found. Any suggestions on how to fix/debug this?

Upvotes: 0

Views: 1364

Answers (1)

Taylor Rahul
Taylor Rahul

Reputation: 729

You are getting this error because of the file plugin cant resolve the path you have provide.

so basically you need to use one more plugin FilePath which can resolve this path and will provide you new path which can be resolved by the file plugin.

const nativeFilePath = await this.filepath.resolveNativePath(gpkgPath);


return this.file.resolveLocalFilesystemUrl(nativeFilePath).then((fileEntry: any) => {

I have mentioned this file Path plugin URL below. After this change it will work for you :)

https://ionicframework.com/docs/native/file-path

Incase you dont want to do this with async then you can use it like mentioned below

this.filePath.resolveNativePath(gpkgPath)
  .then(nativeFilePath => {
      return this.file.resolveLocalFilesystemUrl(nativeFilePath).then((fileEntry: any) => {

  //// your rest code
  })
  .catch(err => console.log(err));

please check below screenshot its working for me

enter image description here

Upvotes: 1

Related Questions