HarmonyOS 相册选的图片 用fs读不到信息 报错13900002

相册选的图片返回的路径 用ohos.file.fs读不到信息 报错13900002

路径地址 file://media/Photo/4/IMG_1715657943_001/IMG_20240514_113723.jpg

HarmonyOS
2024-08-12 15:47:17
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
Heiang

13900002错误是"没有这个文件或目录"

可参考以下代码

selectPhoto() { 
  const photoSelectOptions = new picker.PhotoSelectOptions(); 
  const photoViewPicker = new picker.PhotoViewPicker(); 
  photoSelectOptions.MIMEType = picker.PhotoViewMIMETypes.IMAGE_TYPE; // 过滤选择媒体文件类型为IMAGE 
  photoSelectOptions.maxSelectNumber = 1; // 选择媒体文件的最大数目 
  photoViewPicker.select(photoSelectOptions).then((photoSelectResult: picker.PhotoSelectResult) => { 
    const fileUri = photoSelectResult.photoUris[0] 
    this.getFileInfo(fileUri) 
  }).catch((err: BusinessError) => { 
    console.error(`Invoke photoViewPicker.select failed, code is ${err.code}, message is ${err.message}`); 
  }) 
} 
async getFileInfo(filePathString: string) { 
  try { 
    let uri = fileUri.getUriFromPath(filePathString) 
    let fileUriObject = new fileUri.FileUri(uri); 
    let filename = fileUriObject.name 
    let phAccessHelper = photoAccessHelper.getPhotoAccessHelper(this.context); 
    let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates(); 
    // 配置查询条件,使用PhotoViewPicker选择图片返回的uri进行查询 
    predicates.equalTo('uri', filePathString) 
    let fetchOption: photoAccessHelper.FetchOptions = { 
      fetchColumns: [], 
      predicates: predicates 
    }; 
    let fetchResult: photoAccessHelper.FetchResult<photoAccessHelper.PhotoAsset> = await phAccessHelper.getAssets(fetchOption) 
    // 得到uri对应的PhotoAsset对象,读取文件的部分信息 
    const asset: photoAccessHelper.PhotoAsset = await fetchResult.getFirstObject(); 
    let logString = `filename:${filename} fileSize:${asset.get(photoAccessHelper.PhotoKeys.SIZE)}` 
    console.log(logString) 
    let resFile = fs.openSync(filePathString, fs.OpenMode.READ_ONLY) 
    console.log('文件信息:' + 'fd:' + resFile.fd + '; ' + 'path:' + resFile.path + '; ' + 'name:' + resFile.name); 
  } catch (e) { 
    console.error(e) 
  } 
}

打印信息如下:

filename:screenshot_20240422_194119.jpg fileSize:293994

文件信息:fd:67; path:/Photo/1/IMG_1713786179_000; name:screenshot_20240422_194119.jpg

分享
微博
QQ
微信
回复
2024-08-12 19:39:13
相关问题
HarmonyOS 相册保存图片设置title报错
381浏览 • 1回复 待解决
fs.mkdirSync报错:No such file or directory
2016浏览 • 1回复 待解决
用户相册, 获取不到albumName
1779浏览 • 1回复 待解决
HarmonyOS 获取系统相册报错
237浏览 • 1回复 待解决
HarmonyOS图片保存相册问题
443浏览 • 1回复 待解决
HarmonyOS 保存图片相册问题
377浏览 • 1回复 待解决
如何读取相册图片
887浏览 • 1回复 待解决
HarmonyOS图片压缩不到指定大小
640浏览 • 1回复 待解决
HarmonyOS 保存图片相册权限问题
294浏览 • 1回复 待解决