如何使用Image加载沙箱路径图片资源

沙箱中的图片资源怎么展示

HarmonyOS
2024-06-11 23:18:58
浏览
收藏 0
回答 2
回答 2
按赞同
/
按时间
OwenOO

使用Image组件, 得到文件沙箱路径后, 调用@ohos.file.fileuri模块的fileuri.getUriFromPath(file.path)将沙箱路径转化为沙箱uri, 传入之后即可正常显示.

示例

import fileUri from '@ohos.file.fileuri'; 
import common from '@ohos.app.ability.common'; 
import fs from '@ohos.file.fs'; 
 
@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 + "/test.png" 
            let arrayBuff = context.resourceManager.getRawFileContentSync("icon.png").buffer 
            console.log('testTag-----',arrayBuff) 
            let file = fs.openSync(filePath,fs.OpenMode.CREATE | fs.OpenMode.READ_WRITE) 
            fs.writeSync(file.fd,arrayBuff) 
            this.uri = fileUri.getUriFromPath(filePath) 
            console.log('testTag-----',filePath) 
          }) 
        Image(this.uri) 
          .width(100) 
          .height(100) 
      } 
      .width('100%') 
    } 
    .height('100%') 
  } 
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
分享
微博
QQ
微信
回复
2024-06-12 23:35:05
wx66e0114324300

这个功能对API版本有要求吗?我实测在API9里加载不出来图片。

分享
微博
QQ
微信
回复
2024-09-11 10:09:56


相关问题
Image组件如何读入沙箱内的图片
3490浏览 • 1回复 待解决
HarmonyOS 图片资源加载
275浏览 • 1回复 待解决
HarmonyOS Image加载图片
356浏览 • 1回复 待解决
png图片使用Image组件加载出现锯齿
2186浏览 • 1回复 待解决
Image组件如何加载网络图片
3214浏览 • 1回复 待解决
HarmonyOS Image图片无法加载
577浏览 • 1回复 待解决
Iamge组件如何加载Graphic的资源图片
7201浏览 • 1回复 待解决