盘点鸿蒙应用开发之“分享”功能解决方案
·
注意事项:
当前模块所有接口仅支持Harmony NEXT,暂不支持在openHarmony 应用开发上实现
| 需求 | 首选方案 | 替代方案 |
|---|---|---|
| 纯文本/单图/链接 | ① Share Kit | ② URI |
| 目录/批量/>200 MB | ③ FD | — |
| 网页/短信/二维码跳转 | ④ App Linking | — |
| 个性化海报 | ⑤ 自定义 → Share Kit | ② URI |
| 接收方是自家 App | ② URI / ③ FD | ④ App Linking |
① 系统级 Share Kit(文本/图片/文件通用)
场景:把文本 / 图片 / 文件 / 链接一次性投给微信、邮件、蓝牙等任意支持的应用;无需关心接收方包名,
最低支持 API 12;分享目录或 200 MB+ 大文件请改用 FD 方案。
// 分享文本
import { systemShare } from '@kit.ShareKit';
async function shareText(msg: string) {
const record: systemShare.ShareRecord = {
mimeType: systemShare.MIME_TYPE_TEXT,
data: msg
};
await systemShare.share({ shareRecords: [record] });
}
// 分享图片(uri 为文件路径转成的系统 URI)
async function shareImage(uri: string) {
const record: systemShare.ShareRecord = {
mimeType: 'image/jpeg',
data: uri
};
await systemShare.share({ shareRecords: [record] });
}
② URI 单文件临时分享(发送端)
场景:聊天内发图、发日志;退出接收方即权限失效,安全且简单。
import { fileUri, wantConstant } from '@kit.CoreFileKit';
import { UIAbility, Want } from '@kit.AbilityKit';
async function sendFileByUri(fileName: string) {
const uri = fileUri.getUriFromPath(this.context.filesDir + `/${fileName}`);
const want: Want = {
action: 'ohos.want.action.SEND_DATA',
uri,
type: 'image/jpeg', // 按实际 MIME 修改
flags: wantConstant.Flags.FLAG_AUTH_READ_URI_PERMISSION |
wantConstant.Flags.FLAG_AUTH_WRITE_URI_PERMISSION
};
await this.context.startAbility(want);
}
③ URI 单文件临时分享(接收端)
import { fileIo } from '@kit.CoreFileKit';
import { UIAbility, Want } from '@kit.AbilityKit';
onNewWant(want: Want) {
if (!want.uri) return;
const fd = fileIo.openSync(want.uri, fileIo.OpenMode.READ_ONLY);
// TODO:读文件、展示、落盘
fileIo.closeSync(fd);
}
④ FD 大文件/目录持久分享(发送端)
场景:文件管理器把 3 GB 视频或整个 Documents/ 一次性抛给编辑类应用;权限持续到 FD 关闭。
FD 关闭后文件不可再访问;多线程场景注意生命周期
import { fileIo as fs } from '@kit.CoreFileKit';
import { Want, UIAbility } from '@kit.AbilityKit';
async function sendDirByFd(sourcePath: string) {
const fd = fs.openSync(sourcePath, fs.OpenMode.READ);
const want: Want = {
action: 'ohos.want.action.SEND_DATA',
parameters: { fd, type: 'directory' }
};
await this.context.startAbility(want);
}
⑤ FD 大文件/目录持久分享(接收端)
import { fileIo as fs } from '@kit.CoreFileKit';
import { Want } from '@kit.AbilityKit';
onNewWant(want: Want) {
const fd = want.parameters?.fd as number;
if (!fd) return;
const files = fs.readdirSync(fd); // 遍历目录
// TODO:批量读取
fs.closeSync(fd); // 用完即关
}
⑥ App Linking / Deep Linking(域名跳转)
营销海报扫码直接跳转到指定商品页,并带上优惠券 ID;短信里点链直达聊天窗口。
注意事项:域名必须备案 + HTTPS;想兼容浏览器未装 App,可配置“备用页”自动跳应用市场
// 在 UIAbility 中解析链接参数
onNewWant(want: Want) {
if (want.uri?.startsWith('https://yourdomain/product')) {
const couponId = want.parameters?.couponId as string;
router.pushUrl({ url: `pages/Product?id=${couponId}` });
}
}
⑦ 自定义海报 → 系统分享(截屏+绘制+Share Kit)
游戏结算页“一键生成带二维码的战绩海报”并让用户选微信 / 保存相册。
import { screenshot } from '@ohos.media';
import { image } from '@ohos.graphics';
import { systemShare } from '@kit.ShareKit';
import { fileIo, fileUri } from '@kit.CoreFileKit';
async function sharePoster() {
// 1. 截屏
const pixelMap = await screenshot.getDisplaySnapshot();
// 2. 编码成 jpeg
const packer = image.createImagePacker();
const array = await packer.packing(pixelMap, { format: 'image/jpeg', quality: 95 });
// 3. 落盘
const filePath = `${getContext().filesDir}/poster.jpg`;
fileIo.writeSync(filePath, array);
// 4. 分享
const uri = fileUri.getUriFromPath(filePath);
const record: systemShare.ShareRecord = {
mimeType: 'image/jpeg',
data: uri
};
await systemShare.share({ shareRecords: [record] });
}
⑧ module.json5 必要声明(示例)
"requestPermissions": [
{
"name": "ohos.permission.READ_MEDIA",
"reason": "$string:share_read_media"
},
{
"name": "ohos.permission.FILE_ACCESS",
"reason": "$string:share_large_file"
}
],
"skills": [
{
"actions": ["ohos.want.action.SEND_DATA"],
"uris": [
{ "scheme": "file", "host": "*", "path": "/data/storage/el1/base/files/*" }
]
},
{
"actions": ["ohos.want.action.VIEW"],
"uris": [
{ "scheme": "https", "host": "yourdomain", "path": "/product" }
]
}
]
更多推荐


所有评论(0)