HarmonyOS 将单个应用沙盒中的文件传出到手机中

应用A目录中有个名为8Mine.pdf的文件,路径如下

let context = LOGlobalContext.getAbilityContext()
let filesDir = context.filesDir;
let  destFile =  filesDir + '/1111/111/8Mine.pdf'

我怎样可以将这个文件存储到手机系统中,然后在手机'文件' App中可以看到这个文件

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

可以参考下这个demo

import { common } from '@kit.AbilityKit';
import { BusinessError, request } from '@kit.BasicServicesKit';
import fs from '@ohos.file.fs';
import { buffer } from '@kit.ArkTS';
import { picker, storageStatistics } from '@kit.CoreFileKit';
import { webview } from '@kit.ArkWeb';

let context = getContext(this) as common.UIAbilityContext;

@Entry
@Component
struct testPdfSave {
  testIndex: number = 0;

  build() {
    Column() {
      Button('保存').onClick(() => {
        let filePath = context.cacheDir + "/test" + this.testIndex + ".pdf";
        this.testIndex = this.testIndex + 1
        let url =
          "https://gitee.com/harmonyos-cases/cases/raw/master/CommonAppDevelopment/xxxxx/webpdfviewer/src/main/resources/rawfile/sample.pdf"
        try {
          request.downloadFile(context, {
            url: url,
            background: true,
            filePath: filePath
          }).then((data: request.DownloadTask) => {
            let downloadTask: request.DownloadTask = data;

            downloadTask.on('complete', () => {
              console.info('download complete');
              let file = fs.openSync(filePath, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE);
              let arrayBuffer = new ArrayBuffer(4096000);
              let readLen = fs.readSync(file.fd, arrayBuffer);
              let buf = buffer.from(arrayBuffer, 0, readLen);
              console.info(`content of File: ${buf.toString()}`);
              let writeLen = fs.writeSync(file.fd, arrayBuffer);
              fs.closeSync(file);

              const documentSaveOptions = new picker.DocumentSaveOptions(); // 创建文件管理器选项实例
              documentSaveOptions.newFileNames = ["1.pdf"]; // 保存文件名(可选)
              documentSaveOptions.fileSuffixChoices = ['']; // 保存文件类型(可选)
              let uris: Array<string> = [];
              const documentViewPicker = new picker.DocumentViewPicker(); // 创建文件选择器实例
              documentViewPicker.save(documentSaveOptions).then((documentSaveResult: Array<string>) => {
                uris = documentSaveResult;
                console.info('documentViewPicker.save to file succeed and uris are:' + uris);
                let uri = uris[0];
                let file2 = fs.openSync(uri, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE);
                let writeLen2 = fs.writeSync(file2.fd, arrayBuffer);
                fs.closeSync(file2);
              }).catch((err: BusinessError) => {
                console.error(`Invoke documentViewPicker.save failed, code is ${err.code}, message is ${err.message}`);
              })
            })

            let failCallback = () => {
              console.info('Download task fail.');
            };
            downloadTask.on('fail', failCallback);

          }).catch((err: BusinessError) => {
            console.error(`downLoadFileTask failed, code is ${err.code}, message is ${err.message}`);
          });
        } catch (error) {
          console.error(`downLoadFileTask failed, code is ${error.code}, message is ${error.message}`);
        }
      })
    }
    .height('100%')
    .width('100%')
  }
}
分享
微博
QQ
微信
回复
1天前
相关问题
HarmonyOS 如何加载本地盒中html
85浏览 • 1回复 待解决
HSP文件如何安装到手机
2208浏览 • 1回复 待解决
分享沙箱文件,应用可分享单个文件
1094浏览 • 1回复 待解决
HarmonyOS 获取不到手机
360浏览 • 1回复 待解决
单个文件夹空间统计
870浏览 • 1回复 待解决
HarmonyOS 盒路径视频导出至相册
20浏览 • 1回复 待解决
HarmonyOS如何把图片保存到手机相册
545浏览 • 1回复 待解决