NOT_FOUND_ERR 尝试从 android 设备加载 Ionic 文件作为 blob

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

我在尝试使用 cordova-plugin-file 从我的 android 设备加载文件时收到 FileError {code: 1}。我的代码:

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

屏幕截图 1 - 设备上的文件:

屏幕截图 2 - gpkg-blob:

文件显然找到了。关于如何 fix/debug 有什么建议吗?

您收到此错误是因为文件插件无法解析您提供的路径。

所以基本上你需要使用一个插件 FilePath 它可以解析这个路径并为你提供可以由文件插件解析的新路径。

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


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

我在下面提到了这个文件路径插件 URL。进行此更改后,它将对您有用:)

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

如果您不想使用异步执行此操作,那么您可以像下面提到的那样使用它

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

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

请检查下面的屏幕截图是否适合我