相册路径是file://media/Photo/2/IMG_1712804096_001/IMG_20240411_105316.jpg,要怎么变成绝对路径。

相册路径是file://media/Photo/2/IMG_1712804096_001/IMG_20240411_105316.jpg,要怎么变成绝对路径。

业务代码逻辑里面从相册选择图片 上传到云端服务器相册路径是file://media/Photo/2/IMG_1712804096_001/IMG_20240411_105316.jpg,要怎么变成绝对路径。

获取uri直接用

let stream = fs.createStreamSync(filePath, “r+”);

会报错,文件不存在


HarmonyOS
2024-08-04 18:27:45
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
pumayze

使用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
微信
回复
2024-08-05 12:58:04
相关问题
如何获取文件绝对路径
2186浏览 • 1回复 待解决
HarmonyOS中如何获取资源的绝对路径
549浏览 • 1回复 待解决
HarmonyOS怎么获取系统相册路径
412浏览 • 1回复 待解决