HarmonyOS 图片文件下载

HarmonyOS
2024-12-20 17:14:42
浏览
收藏 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');
  }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
//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%')
  }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
分享
微博
QQ
微信
回复
2024-12-20 20:21:49
相关问题
HarmonyOS 文件或者图片下载
554浏览 • 1回复 待解决
HarmonyOS 如何下载图片
742浏览 • 1回复 待解决
HarmonyOS 文件下载,预览
809浏览 • 1回复 待解决
HarmonyOS 文件下载问题
892浏览 • 1回复 待解决
HarmonyOS 文件下载相关
783浏览 • 1回复 待解决
HarmonyOS 图片下载相关
682浏览 • 1回复 待解决
HarmonyOS 怎么下载图片并显示
704浏览 • 1回复 待解决
HarmonyOS下载文件报错
1373浏览 • 1回复 待解决
HarmonyOS 文件下载保存问题
1401浏览 • 1回复 待解决
HarmonyOS axios下载文件问题
1008浏览 • 1回复 待解决
HarmonyOS下载文件失败返回
1406浏览 • 1回复 待解决
如何查看HarmonyOS下载文件
1132浏览 • 1回复 待解决
HarmonyOS 下载文件到指定文件
998浏览 • 1回复 待解决
HarmonyOS 如何下载文件到本地
870浏览 • 1回复 待解决
HarmonyOS 文件资源动态下载能否实现
526浏览 • 1回复 待解决
HarmonyOS 文件下载的几个问题
839浏览 • 1回复 待解决
request下载文件不能覆盖现有文件
2616浏览 • 1回复 待解决
HarmonyOS 下载文件相关的demo
675浏览 • 1回复 待解决