使用文件管理器选择文件并放到分布式目录

使用文件管理器选择文件并放到分布式目录

HarmonyOS
2024-08-07 10:05:08
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
油炸帕尼尼
documentSelect(fileType: number): void {
    try {
      let DocumentSelectOptions = new picker.DocumentSelectOptions();
      let documentPicker = new picker.DocumentViewPicker();
      documentPicker.select(DocumentSelectOptions).then((DocumentSelectResult: Array<string>) => {
        for (let documentSelectResultElement of DocumentSelectResult) {
          let buf = new ArrayBuffer(CommonConstants.FILE_BUFFER_SIZE);
          let readSize = 0;
          let file = fileIo.openSync(documentSelectResultElement, fileIo.OpenMode.READ_ONLY);
          let readLen = fileIo.readSync(file.fd, buf, { offset: readSize });
          // File name is not supported chinese name.
          let fileName = file.name;
          if (!fileName.endsWith(imageIndex[fileType].fileType) ||
            new RegExp("\[\\u4E00-\\u9FA5]|[\\uFE30-\\uFFA0]", "gi").test(fileName)) {
            promptAction.showToast({
              message: $r('app.string.alert_message_chinese')
            })
            return;
          }
          let destination = fileIo.openSync(getContext()
            .filesDir + '/' + fileName, fileIo.OpenMode.READ_WRITE | fileIo.OpenMode.CREATE);
          let destinationDistribute = fileIo.openSync(getContext()
            .distributedFilesDir + '/' + fileName, fileIo.OpenMode.READ_WRITE | fileIo.OpenMode.CREATE);
          while (readLen > 0) {
            readSize += readLen;
            fileIo.writeSync(destination.fd, buf);
            fileIo.writeSync(destinationDistribute.fd, buf);
            console.info(destinationDistribute.path);
            readLen = fileIo.readSync(file.fd, buf, { offset: readSize });
          }
          fileIo.closeSync(file);
          fileIo.closeSync(destination);
          fileIo.closeSync(destinationDistribute);
          this.appendix.push({ iconIndex: fileType, fileName: fileName });
        }
        Logger.info(`DocumentViewPicker.select successfully, DocumentSelectResult uri: ${JSON.stringify(DocumentSelectResult)}`);
      }).catch((err: BusinessError) => {
        Logger.error(`DocumentViewPicker.select failed with err: ${JSON.stringify(err)}`);
      });
    } catch (error) {
      let err: BusinessError = error as BusinessError;
      Logger.error(`DocumentViewPicker failed with err: ${JSON.stringify(err)}`);
    }
  }
分享
微博
QQ
微信
回复
2024-08-07 14:55:23
相关问题
分布式数据管理没有了吗?
3111浏览 • 1回复 待解决
如何读取读取分布式路径下的文件
350浏览 • 1回复 待解决
如何打开文件管理器有知道的吗?
2227浏览 • 1回复 待解决
分布式键值数据库使用分享
1184浏览 • 1回复 待解决