const contextFaker: Context = getContext(this);
let filePath = contextFaker.filesDir + '/exampleImage.jpg'; // 仅为示例 请替换正确的文件路径
// 获取精准的utd类型
let utdTypeId = utd.getUniformDataTypeByFilenameExtension('.jpg', utd.UniformDataType.IMAGE);
let shareData: systemShare.SharedData = new systemShare.SharedData({
utd: utdTypeId,
uri: fileUri.getUriFromPath(filePath),
title: '图片标题', // 不传title字段时,显示图片文件名
description: '图片描述', // 不传description字段时,显示图片大小
// thumbnail: new Uint8Array() // 优先使用传递的缩略图预览 不传则默认使用原图做预览图
});
在分享图片功能中需要传递contextFaker.filesDir+文件路径,是否可以传递media文件夹中的图片或应该将该文件放在应用哪个目录中可方便的转换为文件的uri?
可以将media下文件写入沙箱或者将图片放在rawfile目录下写入沙箱,再通过contextFaker.filesDir获取。参考以下文档:https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/js-apis-resource-manager-V5\#getmediacontent10
https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/js-apis-resource-manager-V5\#getrawfilecontent9
参考示例如下: