【问题标题】:How to save image from url into assets folder如何将图像从 url 保存到 assets 文件夹中
【发布时间】:2020-11-19 15:04:54
【问题描述】:

我正在使用 Ionic 和 Cordova 开发我的第一个 Android 应用程序,我需要从 REST api 检索图像并将其存储在我的设备上,这样即使设备处于离线状态我也可以使用它。

这是我目前使用的版本:

离子:

Ionic CLI:6.10.1 (C:\Users\agusd\AppData\Roaming\npm\node_modules@ionic\cli) 离子框架:@ionic/angular 5.3.1 @angular-devkit/build-angular:0.901.12 @angular-devkit/原理图:9.1.12 @角/cli:9.1.12 @ionic/angular-toolkit:2.3.0

电容器:

电容器 CLI:2.4.0 @电容器/核心:2.4.0

科尔多瓦:

Cordova CLI:9.0.0 (cordova-lib@9.0.1) Cordova 平台:安卓 8.1.0 Cordova 插件:cordova-plugin-ionic-keyboard 2.2.0、cordova-plugin-ionic-webview 4.2.1(和其他 4 个插件)

我正在遵循适用于 Ionic 3 的指南,但无法使其在我当前的 Ionic 版本上运行。

到目前为止,我已经设法获取了包含 URL 的 JSON 对象,但我不知道如何下载该图像并将其存储到我的资产文件夹中。

我的代码是这样的:

api.service.ts:

apiURL = 'my api url...';
 getDiarios() {
    const diarioArgs = {
      method: 'GET',
      mode: 'cors',
      headers: {
        'Content-Type': 'application/json' }
    };
    return this.http.get(this.apiURL, diarioArgs);
  }

然后我有这个函数,它调用服务并检索响应:

data: any;
lastDiario: any;
lastID = 0;

loadLastDiario() {
    try {
      this.apiService.getDiarios().subscribe(
        (data) => {
          this.data = data;
          for (const item of this.data.content) {
            if (item.id > this.lastID) {
              // I get the object with the largest id, which means is the last uploaded
              this.lastID = item.id;
              this.lastDiario = item;
            }
          }
          console.log(this.lastDiario);
          for (const item of this.lastDiario.archivos) {
            // Here is where I need to save every item of "archivos" into my assets folder or any other
            // Every item has an URL property that I can get through **this.lastDiario.archivos.url**
          }
        }
      );
    } catch (error) {
      console.log("Couldn't connect to the service")
    }
  }

如果有人可以帮助我,我一般是 Ionic 和 Angular 的新手,所以任何建议都会有所帮助!

【问题讨论】:

标签: android angular typescript cordova ionic-framework


【解决方案1】:

assets 文件夹在运行时是只读的。

您只能在设计时将文件存储在其中。

【讨论】:

  • 我明白了,那么我可以在哪里存储我的图像?
  • 看看getExternalFilesDir(null)。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-04-01
  • 2016-09-01
  • 2012-07-28
  • 2019-09-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多