簡體   English   中英

如何將 url 中的圖像保存到資產文件夾中

[英]How to save image from url into assets folder

I am using Ionic with Cordova to develop my first Android app and i need to retrieve an image from a REST api and store it on my device, so then i can use it even if the device is offline.

這是我正在使用的當前版本:

離子:

離子 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/schematics:9.1.12 @angular/cli:9.1.12 @ionic/angular-toolkit:2.3.0

電容器:

電容器 CLI:2.4.0 @capacitor/core:2.4.0

Cordova:

Cordova CLI: 9.0.0 (cordova-lib@9.0.1) Cordova Platforms: android 8.1.0 Cordova Plugins: cordova-plugin-ionic-keyboard 2.2.0, cordova-plugin-ionic-webview 4.2.1, (and 4其他插件)

我正在遵循適用於 Ionic 3 的指南,但我無法讓它在我當前的 Ionic 版本上運行。

到目前為止,我已經設法獲得了包含 URL 的 JSON object,但我不知道如何下載該圖像並將其存儲到我的資產文件夾中。

我的代碼是這樣的:

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

然后我有這個 function,它調用服務並檢索響應:

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 的新手,所以任何建議都會有所幫助!

assets 文件夾在運行時只讀。

您只能在設計時將文件存儲在其中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM