HarmonyOS 图片文件下载

HarmonyOS
1天前
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
FengTianYa

图片下载demo如下,文件下载也可参考此demo,文件上传下载参考链接:https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/app-file-upload-download-V5

//EntryAbility.ets
import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { window } from '@kit.ArkUI';
import common from '@ohos.app.ability.common';
import fs from '@ohos.file.fs';
import request from '@ohos.request';
import { BusinessError } from '@ohos.base';
import buffer from '@ohos.buffer';

export default class EntryAbility extends UIAbility {
  onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void {
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate');
  }

  onDestroy(): void {
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onDestroy');
  }

  onWindowStageCreate(windowStage: window.WindowStage): void {
    let filesDir = this.context.filesDir
    console.log(filesDir)
    console.log("file dir ==> " + filesDir)
    try {
      request.downloadFile(this.context, {
        url: 'xxx',
        filePath: filesDir + '/1.jpg'
      }).then((downloadTask: request.DownloadTask) => {
        downloadTask.on('complete', () => {
          console.info('download complete');
          let file = fs.openSync(filesDir + '/1.jpg', fs.OpenMode.READ_WRITE);
          let arrayBuffer = new ArrayBuffer(1024);
          let readLen = fs.readSync(file.fd, arrayBuffer);
          let buf = buffer.from(arrayBuffer, 0, readLen);
          console.info(`The content of file: ${buf.toString()}`);
          fs.stat(filesDir + '/1.jpg').then((stat: fs.Stat) => {
            console.info("get file info succeed, the size of file is " + stat.size);
          }).catch((err: BusinessError) => {
            console.error("get file info failed with error message: " + err.message + ", error code: " + err.code);
          });
          fs.closeSync(file);
        })
      }).catch((err: BusinessError) => {
        console.error(`Invoke downloadTask failed, code is ${err.code}, message is ${err.message}`);
      });
    } catch (error) {
      let err: BusinessError = error as BusinessError;
      console.error(`Invoke downloadFile failed, code is ${err.code}, message is ${err.message}`);
    }
    // Main window is created, set main page for this ability
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate');

    windowStage.loadContent('pages/Index', (err) => {
      if (err.code) {
        hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? '');
        return;
      }
      hilog.info(0x0000, 'testTag', 'Succeeded in loading the content.');

    });
  }

  onWindowStageDestroy(): void {
    // Main window is destroyed, release UI related resources
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageDestroy');
  }

  onForeground(): void {
    // Ability has brought to foreground
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onForeground');
  }

  onBackground(): void {
    // Ability has back to background
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onBackground');
  }
}
//Index.ets
import { common, Want } from '@kit.AbilityKit';
import fileuri from '@ohos.file.fileuri'
import { image } from '@kit.ImageKit';

@Entry
@Component
struct Index {
  @State pixelMap: image.PixelMap | undefined = undefined
  async loadPicture() {
    const imageSource: image.ImageSource = image.createImageSource('/data/storage/el2/base/haps/entry/files/1.jpg');
    this.pixelMap = await imageSource.createPixelMap();
  }

  build() {
    Column() {
      Button('加载图片')
        .onClick(() => {
          this.loadPicture()
        })
      Image(this.pixelMap)
        .width(200)
        .height(200)
    }
    .width('100%')
  }
}
分享
微博
QQ
微信
回复
1天前
相关问题
HarmonyOS 文件下载保存问题
33浏览 • 1回复 待解决
HarmonyOS下载文件报错
476浏览 • 1回复 待解决
HarmonyOS下载文件失败返回
477浏览 • 1回复 待解决
如何查看HarmonyOS下载文件
336浏览 • 1回复 待解决
request下载文件不能覆盖现有文件
1836浏览 • 1回复 待解决
HarmonyOS web下载文件点击无反应
66浏览 • 1回复 待解决
HarmonyOS 如何监听下载文件的进度
31浏览 • 1回复 待解决
是否支持iframe下载文件
797浏览 • 1回复 待解决
如何使用接口下载文件
2105浏览 • 1回复 待解决
HarmonyOS 保存图片文件异常
333浏览 • 1回复 待解决
HarmonyOS 下载文件并保存到指定目录
38浏览 • 1回复 待解决
image组件是否支持图片下载链接
1696浏览 • 1回复 待解决
PolarDB数据备份文件怎么下载
3042浏览 • 1回复 待解决
下载文件打印乱码如何修改
369浏览 • 1回复 待解决
对指定url的图片进行下载保存
1073浏览 • 1回复 待解决
HarmonyOS web上传文件图片
255浏览 • 1回复 待解决