HarmonyOS 如何保存一张远程图片到本地文件的公共目录的DOWNLOAD文件夹

如何保存一张远程图片到本地文件的公共目录的DOWNLOAD文件夹,保存一张http图片到本地的公共目录的DOWNLOAD文件夹,参考了

https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/save-user-file-V5#保存文档类文件

https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/save-user-file-V5#download模式保存文件

均未成功实现。希望帮忙修改一下。

最小化 demo如下:

import picker from '@ohos.file.picker';
import fs from '@ohos.file.fs';
import { BusinessError } from '@ohos.base';
import common from '@ohos.app.ability.common';
import { request } from '@kit.BasicServicesKit';


@Entry
@Component
struct Index {
  aboutToAppear(): void {
    let uri: string = '';
    let context = getContext(this) as common.Context;
    const tempDir = context.tempDir;
    const documentViewPicker = new picker.DocumentViewPicker(context);
    const documentSaveOptions = new picker.DocumentSaveOptions();
    documentSaveOptions.pickerMode = picker.DocumentPickerMode.DOWNLOAD;
    documentViewPicker.save(documentSaveOptions).then((documentSaveResult: Array<string>) => {
      uri = documentSaveResult[0];
      let tmpfile = `${tempDir}/${Date.now()}.png`
      request.downloadFile(context, {
        url: `https://图片资源路径`,
        filePath: tmpfile
      }).then((downloadTask: request.DownloadTask) => {
        downloadTask.on('complete', () => {
          console.info('download complete');
          let file = fs.openSync(tmpfile, fs.OpenMode.READ_WRITE);
          let buf = new ArrayBuffer(fs.statSync(file.fd).size);
          fs.readSync(file.fd, buf);
          fs.copyFileSync(tmpfile, `${uri}/${Date.now()}.png`, fs.OpenMode.CREATE | fs.OpenMode.READ_WRITE)
          fs.closeSync(file);
        })
      }).catch((err: BusinessError) => {
        console.error(`Invoke downloadTask failed, code is ${err.code}, 1message is ${err.message}`);
      });
      console.info('documentViewPicker.save succeed and uri is:' + uri);
    }).catch((err: BusinessError) => {
      console.error(`Invoke documentViewPicker.save failed, code is ${err.code}, message is ${err.message}`);
    })
  }
  build() {
  }
}
HarmonyOS
3天前
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
zbw_apple

目前不支持download模式直接去调fs保存文件,这样拿到的uri是没有操作权限的;需要下载文件到公共路径还是推荐下载后picker保存到指定路径的方式

copyToDownload() {
  let context = getContext() as common.UIAbilityContext;
  let filesDir = context.filesDir;
  try {
    request.downloadFile(context, {
      url: 'https://xxx',
      filePath: filesDir + '/test.docx'
    }).then((downloadTask: request.DownloadTask) => {

      downloadTask.on('complete', () => {
        let documentSaveOptions = new picker.DocumentSaveOptions();
        documentSaveOptions.newFileNames = ['test.docx'];
        let documentPicker = new picker.DocumentViewPicker();
        documentPicker.save(documentSaveOptions).then((documentSaveResult: Array<string>) => {
          let uri = documentSaveResult[0];
          let sanFile = fs.openSync(getContext().filesDir + '/test.docx', fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE)
          let pubFile = fs.openSync(uri, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE)
          fs.copyFileSync(sanFile.fd, pubFile.fd)
          console.info('DocumentViewPicker.save successfully, DocumentSaveResult uri: ' +
          JSON.stringify(documentSaveResult));
        }).catch((err: BusinessError) => {
          console.error('DocumentViewPicker.save failed with err: ' + JSON.stringify(err));
        });
      })
    }).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}`);
  }
}
分享
微博
QQ
微信
回复
3天前
相关问题
如何保存一张PNG图片相册中
1876浏览 • 1回复 待解决
HarmonyOS 获取手机图片文件夹
288浏览 • 0回复 待解决
如何吸取一张图片色值?
429浏览 • 1回复 待解决
HarmonyOS 图片文件夹获取封面图coverUri
304浏览 • 1回复 待解决
怎样在根目录中创建文件夹
4154浏览 • 1回复 待解决
如何打开指定文件夹,选择文件返回
8751浏览 • 1回复 待解决
HarmonyOS 获取手机最新一张图片
19浏览 • 1回复 待解决
打包包含ohosTest文件夹和test文件夹
454浏览 • 1回复 待解决