HarmonyOS Image加载沙箱中的图片未显示?

我在沙箱中下载了一张图片, 路径如下, 且通过Device File Browser查看了此文件存在

/data/storage/el2/base/haps/MainEntry/files/IM/Images/1f23c971ed19e3a5cf7d5710faea359a.png

但是我通过Image(此沙箱图片的路径), 未显示, 请问沙箱中的图片是怎么通过Image加载出来的

阅读 460
1 个回答

通过调用@ohos.file.fileuri模块的fileuri.getUriFromPath(file.path)将沙箱路径转化为沙箱uri,就可以加载出来了。

示例代码如下:

import fileUri from '@ohos.file.fileuri';
import common from '@ohos.app.ability.common';

@Entry
@Component
struct Index {
  @State message: string = 'Hello World';
  @State uri: string = ""

  build() {
    Row() {
      Column() {
        Text(this.message)
          .fontSize(50)
          .fontWeight(FontWeight.Bold)
          .onClick(() => {
            let context = getContext(this) as common.UIAbilityContext
            let filePath = context.filesDir + "/IM/icon.png"
            console.log('filePath-----',filePath)
            this.uri = fileUri.getUriFromPath(filePath)
            console.log('uri-----',this.uri)
          })
        Image(this.uri)
          .width(100)
          .height(100)
      }
      .width('100%')
    }
    .height('100%')
  }
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进