鸿蒙开发核心知识点,看这篇文章就够了
最新版!鸿蒙HarmonyOS Next应用开发实战学习路线
鸿蒙HarmonyOS NEXT开发技术最全学习路线指南
鸿蒙应用开发实战项目,看这一篇文章就够了(部分项目附源码)
本示例介绍使用Share Kit和ShareExtensionAbility 实现从图库分享图片到应用的场景。该场景多用于聊天类应用。
使用说明
// 构造ShareData,需配置一条有效数据信息
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() // 优先使用传递的缩略图预览 不传则默认使用原图做预览图
});
shareData.addRecord({
utd: utdTypeId,
uri: fileUri.getUriFromPath(filePath),
title: '图片标题', // 不传title字段时,显示图片文件名
description: '图片描述', // 不传description字段时,显示图片大小
});
// 进行分享面板显示
let controller: systemShare.ShareController = new systemShare.ShareController(shareData);
let context = getContext(this) as common.UIAbilityContext;
controller.show(context, {
selectionMode: systemShare.SelectionMode.SINGLE,
previewMode: systemShare.SharePreviewMode.DETAIL,
}).then(() => {
console.info('ShareController show success.');
}).catch((error: BusinessError) => {
console.error(`ShareController show error. code: ${error.code}, message: ${error.message}`);
});
"skills": [
{
"entities": [
"entity.system.home"
],
"actions": [
"action.system.home",
"ohos.want.action.sendData"
],
"uris": [
{
"scheme": "file",
"utd": "general.image",
"maxFileSupported": 1
}
]
}
]
onNewWant(want: Want, launchParam: AbilityConstant.LaunchParam): void {
if (want.parameters!['ability.params.stream'] !== undefined) {
AppStorage.setOrCreate('imageUri', want.parameters!["ability.params.stream"].toString());
}
}
onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void {
systemShare.getSharedData(want)
.then((data: systemShare.SharedData) => {
data.getRecords().forEach((record: systemShare.SharedRecord) => {
// 处理分享数据
AppStorage.setOrCreate('imageUri', record.uri)
});
})
}
aboutToAppear() {
if (AppStorage.get('imageUri') !== undefined) {
this.shareImageUri = AppStorage.get('imageUri');
this.textDetailData.push({
profilePicture: $r('app.media.photo0'),
shareImageUri: this.shareImageUri,
content: ''
});
// 通知lazyForeach重新加载数据
this.dataSource.modifyAllData(this.textDetailData);
}
}
不涉及
shareimagepage // har类型
|---components
| |---ShareImagePage.ets // 视图层-分享页面
| |---ListDataSource.ets // 数据模型层-聊天列表数据