HarmonyOS 拍照最佳实现方案里面解析的photoUri无法用于Image组件展示吗

根据拍照最佳实践里面的photoUri打印出来为沙箱地址 //file://media/Photo/20/IMG_1719535410_015/1719535310081.jpg

将这个地址赋予Image组件无法显示

但是new picker.PhotoViewPicker(context).select选取的本地图片也是沙箱地址 //file://media/Photo/12/IMG_1719215593_009/IMG_20240624_155133_1.jpg

将这个地址赋予Image组件可以显示

请问是拍照获取的照片沙箱地址还需要做什么处理吗

HarmonyOS
2024-12-27 16:18:09
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
shlp

使用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 = ""
  @State path: 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"
            console.log('filepath-----',filePath)
            this.path = filePath
            let arrayBuff = context.resourceManager.getRawFileContentSync("img.png").buffer
            let file = fs.openSync(filePath,fs.OpenMode.CREATE | fs.OpenMode.READ_WRITE)
            fs.writeSync(file.fd,arrayBuff)
            this.uri = fileUri.getUriFromPath(filePath)
            console.log('filepath1-----',filePath)
          })
        Image(this.uri)
          .objectFit(ImageFit.Contain)
          .width(200)
          .height(200)
      }
      .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.
  • 36.
  • 37.
分享
微博
QQ
微信
回复
2024-12-27 17:54:24
相关问题
引导遮罩效果实现最佳方案
2127浏览 • 1回复 待解决
HarmonyOS 地图组件无法展示
888浏览 • 1回复 待解决
地图组件无法正常展示
1500浏览 • 1回复 待解决
Image组件无法设置长按事件
2704浏览 • 1回复 待解决
HarmonyOS Image 组件支持加载缓存
708浏览 • 1回复 待解决
HarmonyOS Image 组件有缓存功能
1281浏览 • 1回复 待解决
HarmonyOS Image组件无法显示网络图片
2398浏览 • 1回复 待解决