HarmonyOS 鸿蒙Next:Image如何加载应用沙箱路径下的图片资源

发布于 1周前 作者 sinazl 来自 鸿蒙OS

HarmonyOS 鸿蒙Next:Image如何加载应用沙箱路径下的图片资源

Image如何加载应用沙箱路径下的图片资源

3 回复

demo如下:

import Want from ‘[@ohos](/user/ohos).app.ability.Want’ 
import photoAccessHelper from ‘[@ohos](/user/ohos).file.photoAccessHelper’; 
import { BusinessError } from ‘[@ohos](/user/ohos).base’; 
import uri from ‘[@ohos](/user/ohos).uri’; 

@Entry @Component struct PhotoPicker { @State isShow: boolean = false; @State uri: string = ‘’; build() { Row() { Column() { Image(this.uri).height(‘10%’).width(‘50%’) Button(‘单选’).height(‘10%’).width(‘50%’).onClick(() => { this.example02(); }) } .width(‘100%’) } .height(‘100%’) }

async example02() { try { console.info(1111111picker start); const photoSelectOptions = new photoAccessHelper.PhotoSelectOptions(); photoSelectOptions.MIMEType = photoAccessHelper.PhotoViewMIMETypes.IMAGE_TYPE; photoSelectOptions.maxSelectNumber = 1; // 选择媒体文件的最大数目 const photoViewPicker = new photoAccessHelper.PhotoViewPicker(); photoViewPicker.select(photoSelectOptions).then((photoSelectResult: photoAccessHelper.PhotoSelectResult) => { console.info(11111111picker success ${photoSelectResult.photoUris}); this.uri = photoSelectResult.photoUris[0]; }).catch((err: BusinessError) => { console.info(11111111picker fail); console.info(Invoke photoViewPicker.select failed, code is ${err.code}, message is ${err.message}); }) console.info(11111111picker end); } catch (error) { } } }<button style="position: absolute; padding: 4px 8px 0px; cursor: pointer; top: 8px; right: 8px; font-size: 14px;">复制</button>

photoAccessHelper跟沙盒有什么关系?

在HarmonyOS鸿蒙Next中,Image组件加载应用沙箱路径下的图片资源,需先将沙箱路径转换为沙箱URI。可通过调用fileuri模块的getUriFromPath方法实现路径到URI的转换。将转换后的URI作为Image组件的source参数即可加载图片。注意遵循鸿蒙系统的安全规范,确保应用的稳定运行及用户数据安全。如果问题依旧没法解决请加我微信,我的微信是itying888。

回到顶部