HarmonyOS fs.access(path)判断路径总是不存在

通过系统选择相册api选择返回的路径path可以通过image组件加载,但是fs.access判断路径总是不存在。

path路径为:file://media/Photo/6/IMG_1722926464_004/IMG_20240806_143924.jpg

示例代码:

import fs from '@ohos.file.fs';

checkFile(path:string) {
  LogUtil.debug("FUXIAOLONG","检查的图片路径:" + path);
  fs.access(path)
    .then((res: boolean) => {
      if (res) {
        LogUtil.debug("FUXIAOLONG","file exists");
      } else {
        LogUtil.debug("FUXIAOLONG","file not exists");
      }
    }).catch((err: BusinessError) => {
    LogUtil.debug("FUXIAOLONG","access failed with error message: " + err.message + ", error code: " + err.code);
    return
  })
}

//选择相册代码:
getAllImg() {
  try {
    let PhotoSelectOptions: picker.PhotoSelectOptions = new picker.PhotoSelectOptions();
    PhotoSelectOptions.MIMEType = picker.PhotoViewMIMETypes.IMAGE_TYPE;
    PhotoSelectOptions.maxSelectNumber = 1;
    let photoPicker: picker.PhotoViewPicker = new picker.PhotoViewPicker();
    photoPicker.select(PhotoSelectOptions).then((PhotoSelectResult: picker.PhotoSelectResult) => {
      this.imgDatas = PhotoSelectResult.photoUris;
      if (this.imgDatas != undefined && this.imgDatas.length > 0) {
        let picUrl = this.imgDatas[0]
        this.checkFile(picUrl)
      }
      console.info('PhotoViewPicker.select successfully, PhotoSelectResult uri: ' + JSON.stringify(PhotoSelectResult));
    }).catch((err: Error) => {
      let message = (err as BusinessError).message;
      let code = (err as BusinessError).code;
      console.error(`PhotoViewPicker.select failed with. Code: ${code}, message: ${message}`);
    });
  } catch (err) {
    let message = (err as BusinessError).message;
    let code = (err as BusinessError).code;
    console.error(`PhotoViewPicker failed with. Code: ${code}, message: ${message}`);
  }
HarmonyOS
1天前
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
FengTianYa

fs.access不支持uri只支持path,需要先使用fs.open接口,再使用fs.access。

分享
微博
QQ
微信
回复
1天前
相关问题
真机运行,UDID不存在
6232浏览 • 1回复 待解决
HarmonyOS 安装时报错依赖的模块不存在
1225浏览 • 1回复 待解决
HarmonyOS 哪些情况下主窗口不存在
144浏览 • 1回复 待解决
如何实现文件不存在则创建文件
2187浏览 • 1回复 待解决
ATS如何判断路径是文件夹还是文件?
750浏览 • 1回复 待解决
HarmonyOS canvas Path 椭圆无法闭合路径
26浏览 • 1回复 待解决