【问题标题】:NOT_FOUND_ERR while trying to load Ionic file as blob from android device尝试将 Ionic 文件作为 blob 从 android 设备加载时出现 NOT_FOUND_ERR
【发布时间】:2021-01-04 19:34:30
【问题描述】:

我在尝试使用cordova-plugin-file 从我的安卓设备加载文件时收到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:

该文件显然已找到。有关如何修复/调试此问题的任何建议?

【问题讨论】:

    标签: javascript android ionic-framework ionic-native


    【解决方案1】:

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

    所以基本上你需要再使用一个插件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));
    

    请检查下面的屏幕截图它对我有用

    【讨论】:

    • filePath 似乎没有帮助:nativeFilePath 是“file:///storage/emulated/0/test_dir/test.gpkg”。 Geopackage localURL 不会改变(应该吗?)。
    • filePath 将在这里为您提供帮助。我已经在我的本地设置了所有东西并试了一下..之前它也不适合我,但是在这个改变之后它开始工作..请试一试
    • 我可能对路径有误,。我确实深入了很多,但是在我可以打赌之后,它会为你工作
    • 在文件路径插件上他们已经明确提到这个插件可以让你resolve the native filesystem path for Android
    • 我仍然得到相同的结果...是否需要一些特殊配置?我阅读了不同的安全策略并将其添加到我的 index.html:&lt;meta http-equiv="Content-Security-Policy" content="default-src 'self' http://localhost:8100 'unsafe-inline' 'unsafe-eval' data: cdvfile://* file://* content://* gap: https://ssl.gstatic.com;"/&gt;
    猜你喜欢
    • 1970-01-01
    • 2018-08-04
    • 2012-03-04
    • 1970-01-01
    • 1970-01-01
    • 2014-07-28
    • 2014-11-19
    • 2012-01-18
    • 1970-01-01
    相关资源
    最近更新 更多