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

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

HarmonyOS
2024-06-11 23:18:58
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
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%') 
  } 
}
分享
微博
QQ
微信
回复
2024-06-12 23:35:05
相关问题
Image组件如何读入沙箱内的图片
1100浏览 • 1回复 待解决
png图片使用Image组件加载出现锯齿
377浏览 • 1回复 待解决
Image组件如何加载网络图片
884浏览 • 1回复 待解决
Image组件如何加载沙盒内的图片
717浏览 • 1回复 待解决
Iamge组件如何加载Graphic的资源图片
5684浏览 • 1回复 待解决
Image图片加载缓慢,有什么方案么?
320浏览 • 1回复 待解决
沙箱路径的文件怎么拿取?
464浏览 • 1回复 待解决
鸿蒙图片资源在Java代码中如何使用
6601浏览 • 1回复 待解决
鸿蒙如何获取资源文件种的路径
14355浏览 • 3回复 待解决