HarmonyOS 如何将一个file://协议的uri转为internal://协议类型

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

使用ohos.file.fs功能模块对文件/目录进行操作前,需要先获取其应用沙箱路径,这边提供一个demo供参考,看是否能解决问题:

import { BusinessError } from '@kit.BasicServicesKit';
import fs from '@ohos.file.fs';
import picker from '@ohos.file.picker';
@Entry
@Component
struct Index {
  @State message: string = 'Hello World';
  async photoPick() {
    try {
      let PhotoSelectOptions = new picker.PhotoSelectOptions();
      PhotoSelectOptions.MIMEType = picker.PhotoViewMIMETypes.IMAGE_TYPE;
      PhotoSelectOptions.maxSelectNumber = 5;
      let photoPicker = new picker.PhotoViewPicker();
      photoPicker.select(PhotoSelectOptions)
        .then(async (PhotoSelectResult: picker.PhotoSelectResult) => {
          console.info('PhotoViewPicker.select successfully, PhotoSelectResult uri: ' + JSON.stringify(PhotoSelectResult));
          let file1 = fs.openSync(PhotoSelectResult.photoUris[0])
          fs.copyFileSync(file1.fd, `data/storage/el2/base/files/${file1.name}`)

          let file2 = fs.openSync(`data/storage/el2/base/files/${file1.name}`, fs.OpenMode.READ_WRITE)
          console.log(`file fd ==> ${file2.fd} | file path ==> ${file2.path}`)
          // fs.closeSync(file1)
          // fs.closeSync(file2)
        }).catch((err: BusinessError) => {
        console.error('PhotoViewPicker.select failed with err: ' + JSON.stringify(err));
      });
    } catch (error) {
      let err: BusinessError = error as BusinessError;
      console.error('PhotoViewPicker failed with err: ' + JSON.stringify(err));
    }
  }

  build() {
    Row() {
      Column() {
        Button(this.message)
          .fontSize(50)
          .fontWeight(FontWeight.Bold)
          .onClick(async () => {
            this.photoPick()
          })
      }
      .width('100%')
    }
    .height('100%')
  }
}
分享
微博
QQ
微信
回复
2天前
相关问题
如何将Map/HashMap转为Object对象?
2007浏览 • 1回复 待解决
如何将多工程hap打包成一个app
1262浏览 • 1回复 待解决
ets中如何将图片转为byte[]?
3276浏览 • 1回复 待解决
如何将JSON字符串转为Map
327浏览 • 1回复 待解决
如何将字符串转为Uint8Array?
792浏览 • 1回复 待解决