经验分享 · 2025年7月11日

Harmony ArkTS中Image()无法显示沙箱中的图片

当给Image组件传入沙箱路径中的图片时,图片不显示,在日志中可以看到一系列错误:

GetAsset failed: data/storage/el2/base/haps/entry/cache/crop_1752232915324.jpg
No asset data! NodeId = 269-243.
Fail load imageData. src = <private>, nodeId = 269-243.
Image LoadFail

根本原因:
Image组件不能直接传入应用沙箱路径,需要传入应用沙箱uri;

解决方案:拿到文件的沙箱路径后,通过调用@ohos.file.fileuri模块的fileuri.getUriFromPath(file.path)将沙箱路径转化为沙箱uri,传入之后即可正常显示。this.ImageUri = fileUri.getUriFromPath(ImagePath);

此api参考链接:https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/js-apis-file-fileuri-V5#ZH-CN_TOPIC_0000001884918402__fileurigeturifrompath