【问题标题】:Ionic 4: How to save images on device for offline mode that comes from firebase storageIonic 4:如何在设备上保存来自 firebase 存储的离线模式的图像
【发布时间】:2019-09-14 03:02:55
【问题描述】:

我使用 SQLite 原生插件 来存储文本内容,但我也需要使用图像来做到这一点! (对于离线模式)

使用 angular/ionic 4 无法将图像转换为 base64 字符串,所以我的问题是:

如何使用 ionic/Cordova 提供的 Native 文件传输插件从 Firebase 提供的图片 url 下载图片

我已经阅读了文档,但它是基本的,并没有提供太多!

DOCS 提供的代码:我不明白file.pdf 的下载方法!图片可以带有任何扩展名,例如 jpg/png ..etc )

download() {
  const url = 'http://www.example.com/file.pdf';
  fileTransfer.download(url, this.file.dataDirectory + 'file.pdf').then((entry) => {
    console.log('download complete: ' + entry.toURL());
  }, (error) => {
    // handle error
  });
}

【问题讨论】:

  • 放置离子信息的快照
  • webview 插件版本是多少?
  • webview 版本 3.2.1 我找到了一个解决方案说安装 1.2.1 但是当我尝试这个时我的应用程序崩溃了,所以我回到了我的最新版本
  • 你试过 normalizeURL().因为它对我有用

标签: javascript angular firebase ionic-framework ionic4


【解决方案1】:

您可以使用 LocalStorage,更多信息在这里:https://ionicframework.com/docs/building/storage

几周前我用来存储图像的示例。

 if (this.platform.is('ios')) {
      this.fileName = 'Image';
       this.filePath = this.file.documentsDirectory.replace(/file:\/\//g, '') + this.fileName;

    } else if (this.platform.is('android')) {
      this.fileName = 'Image';
        this.filePath = this.file.externalDataDirectory.replace(/file:\/\//g, '') + this.fileName;

【讨论】:

  • 我已经通过文件传输插件成功下载了图像,但是当从我的手机中获取 URL 并将其放在图像 src 上时会弹出一个错误说Error: Not allowed to load local resource 那么如何加载图像来了现在通过电话?
【解决方案2】:

Firebase 具有很酷的 offline 功能。 尝试查询必要的数据,并使用它。

否则使用这个plugin

【讨论】:

  • 我的问题是如何使用那个插件?!无论如何,我已经使用它并成功存储了我的图像,但现在我有一个新错误Error: Not allowed to load local resource 将图像的路径更改为电话路径file:////data/user/0/wysbird.ionic.starter/files/F1554080441-cover4.jpg 那么我该如何解决?
【解决方案3】:

感谢您的回复,但我已经想通了!

我已将我的图像存储在手机上,然后使用 Cordova 插件转换为 base64 图像,您可以找到 here 然后我将 base64 图像与文本内容一起添加到我的存储中。

【讨论】:

  • 嗨,我想做同样的事情,你能指导我一下吗..
  • 只需使用此插件将您拥有的任何图像转换为 base 64 格式 ionicframework.com/docs/native/base64 然后使用 SQLite 将输出保存在您的设备存储中,当您离线时使用您的您保存的离线数据
【解决方案4】:

file.pdf 下载方法不是问题,因为您可以从 API 响应中获取带有扩展名的整个文档/图像名称。

例如

for (let i = 0; i < res.data.length; i++) { 
     let fileName = res.data[i].name;
     let url = 'http://www.example.com/' + res.data[i].name;
     const _fileTransfer: FileTransferObject = this._fileTransfer.create();
     _fileTransfer.download(url, this._file.dataDirectory + fileName).then((entry) => {
         console.log('download complete: ' + entry.toURL());
     }, (error) => {
         console.log(error);
     });
}

然后,您可以使用 normalizeURL 显示图像,给定一个表示 URL 的字符串 returns the URL path after stripping the trailing slashes

import { normalizeURL } from 'ionic-angular';

{'filePath': normalizeURL(result[i].fileName)

<img src="{{filePath}}">

【讨论】:

  • 是的,除了normalizeUrl 之外,我所做的已经被ionic 4 弃用了,所以我没有找到在没有not allowed error 的情况下访问文件的方法,所以我不得不转换图像我已经下载到 base64 版本,它可以工作,但对这种方法的性能不满意
  • @heshamshawky 你试过let win: any = window; let safeURL = win.Ionic.WebView.convertFileSrc('file:///myFile/....'); 吗?
  • 我已经在你回复之前用几秒钟 XD 尝试过了,它很有效,非常感谢你的回答是我在这里得到的最好的
猜你喜欢
  • 2016-08-12
  • 1970-01-01
  • 2019-09-10
  • 2017-02-11
  • 2018-10-13
  • 2021-05-15
  • 2019-05-24
  • 2022-08-18
  • 1970-01-01
相关资源
最近更新 更多