Q:开发者在使用系统分享时,会遇到以下几个经典场景:
A:使用systemShare相关API前,需要先引入依赖模块,并获取UIAbilityContext上下文,后续各场景代码均基于以下导入和上下文:
import { systemShare } from '@kit.ShareKit';
import { fileIo, fileUri } from '@kit.CoreFileKit';
import { uniformTypeDescriptor as utd } from '@kit.ArkData';
import { common } from '@kit.AbilityKit';
import { BusinessError } from '@kit.BasicServicesKit';
@Entry
@Component
struct Index {
// 获取UIAbilityContext,用于访问应用文件目录和拉起分享面板
context: common.UIAbilityContext = this.getUIContext().getHostContext() as common.UIAbilityContext;
// ...
} // this.context为UIAbilityContext,需在组件中提前获取
let shareData: systemShare.SharedData = new systemShare.SharedData({
utd: utd.getUniformDataTypeByFilenameExtension('.jpg', utd.UniformDataType.IMAGE),
uri: fileUri.getUriFromPath(this.context.filesDir + '/exampleKnock1.jpg'),
title: 'exampleKnock1',
description: 'Image Description',
});
try {
shareData.addRecord({
utd: utd.getUniformDataTypeByFilenameExtension('.png', utd.UniformDataType.IMAGE),
uri: fileUri.getUriFromPath(this.context.filesDir + '/exampleKnock2.png'),
title: 'exampleKnock2',
description: 'Image Description',
});
} catch (error) {
console.error(`addRecord error. Code: ${error?.code}, message: ${error?.message}`);
} import { BusinessError } from '@kit.BasicServicesKit';
let controller: systemShare.ShareController = new systemShare.ShareController(shareData);
controller.show(this.context, {
selectionMode: systemShare.SelectionMode.SINGLE,
previewMode: systemShare.SharePreviewMode.DETAIL,
excludedAbilities: [systemShare.ShareAbilityType.PRINT, systemShare.ShareAbilityType.COPY_TO_PASTEBOARD,
systemShare.ShareAbilityType.SAVE_AS_FILE, systemShare.ShareAbilityType.SAVE_TO_MEDIA_ASSET]
}).then(() => {
console.info('ShareController show success.');
}).catch((error: BusinessError) => {
console.error(`ShareController show error. code: ${error?.code}, message: ${error?.message}`);
}); import { BusinessError } from '@kit.BasicServicesKit';
private async shareBatch() {
// this.context为UIAbilityContext,需在组件中提前获取
let shareData: systemShare.SharedData = new systemShare.SharedData({
utd: utd.getUniformDataTypeByFilenameExtension('.jpg', utd.UniformDataType.IMAGE),
uri: fileUri.getUriFromPath(this.context.filesDir + '/exampleKnock1.jpg'),
title: 'exampleKnock1',
description: 'Image Description',
});
try {
shareData.addRecord({
utd: utd.getUniformDataTypeByFilenameExtension('.png', utd.UniformDataType.IMAGE),
uri: fileUri.getUriFromPath(this.context.filesDir + '/exampleKnock2.png'),
title: 'exampleKnock2',
description: 'Image Description',
});
} catch (error) {
console.error(`addRecord error. Code: ${error?.code}, message: ${error?.message}`);
}
let controller: systemShare.ShareController = new systemShare.ShareController(shareData);
controller.show(this.context, {
previewMode: systemShare.SharePreviewMode.DETAIL,
selectionMode: systemShare.SelectionMode.BATCH,
}).then(() => {
console.info('ShareController show success.');
}).catch((error: BusinessError) => {
console.error(`ShareController show error. code: ${error?.code}, message: ${error?.message}`);
});
} import { BusinessError } from '@kit.BasicServicesKit';
import { http } from '@kit.NetworkKit';
context = this.getUIContext().getHostContext() as common.UIAbilityContext;
imageBuffer: ArrayBuffer | undefined = undefined;
promptAction: PromptAction = this.getUIContext().getPromptAction();
// 通过http的request方法从网络下载图片资源
getPicture(): Promise<void> {
return new Promise((resolve, reject) => {
http.createHttp()
.request('', // 替换为真实网络图片地址
(error: BusinessError, data: http.HttpResponse) => {
if (error) {
// 下载失败时弹窗提示检查网络,不执行后续逻辑
try {
this.promptAction.showToast({
message: '图片加载失败,请检查网络',
duration: 2000
});
} catch (error) {
console.error(`download error. code: ${error?.code}, message: ${error?.message}`);
}
reject();
return;
}
// 判断网络获取到的资源是否为ArrayBuffer类型
if (data.result instanceof ArrayBuffer) {
this.imageBuffer = data.result as ArrayBuffer;
}
resolve();
}
);
});
}
// 保存到沙箱路径
async shareNetImage(): Promise<void> {
this.imageBuffer = undefined;
await this.getPicture();
try {
const path = this.context.filesDir + '/XXXX'; // XXXX替换为真实文件名
const file = await fileIo.open(path, fileIo.OpenMode.READ_WRITE | fileIo.OpenMode.CREATE);
await fileIo.write(file.fd, this.imageBuffer);
await fileIo.close(file.fd);
// 生成应用URI
const uri = fileUri.getUriFromPath(path);
const data: systemShare.SharedData = new systemShare.SharedData({
utd: utd.UniformDataType.IMAGE,
uri: uri
});
const controller: systemShare.ShareController = new systemShare.ShareController(data);
controller.show(this.context, {
previewMode: systemShare.SharePreviewMode.DETAIL,
selectionMode: systemShare.SelectionMode.SINGLE
}).then(() => {
console.info('ShareController show success.');
}).catch((error: BusinessError) => {
console.error(`ShareController show error. code: ${error?.code}, message: ${error?.message}`);
});
} catch (error) {
console.error(`fileIo error. Code: ${error?.code}, message: ${error?.message}`);
}
} 完整示例代码:
import { systemShare } from '@kit.ShareKit';
import { fileIo, fileUri } from '@kit.CoreFileKit';
import { uniformTypeDescriptor as utd } from '@kit.ArkData';
import { common } from '@kit.AbilityKit';
import { BusinessError } from '@kit.BasicServicesKit';
import { http } from '@kit.NetworkKit';
import { PromptAction } from '@kit.ArkUI';
@Entry
@Component
struct Index {
// Startsolution4
context = this.getUIContext().getHostContext() as common.UIAbilityContext;
imageBuffer: ArrayBuffer | undefined = undefined;
promptAction: PromptAction = this.getUIContext().getPromptAction();
// 通过http的request方法从网络下载图片资源
getPicture(): Promise<void> {
return new Promise((resolve, reject) => {
http.createHttp()
.request('', // 替换为真实网络图片地址
(error: BusinessError, data: http.HttpResponse) => {
if (error) {
// 下载失败时弹窗提示检查网络,不执行后续逻辑
try {
this.promptAction.showToast({
message: '图片加载失败,请检查网络',
duration: 2000
});
} catch (error) {
console.error(`download error. code: ${error?.code}, message: ${error?.message}`);
}
reject();
return;
}
// 判断网络获取到的资源是否为ArrayBuffer类型
if (data.result instanceof ArrayBuffer) {
this.imageBuffer = data.result as ArrayBuffer;
}
resolve();
}
);
});
}
// 保存到沙箱路径
async shareNetImage(): Promise<void> {
this.imageBuffer = undefined;
await this.getPicture();
try {
const path = this.context.filesDir + '/XXXX'; // XXXX替换为真实文件名
const file = await fileIo.open(path, fileIo.OpenMode.READ_WRITE | fileIo.OpenMode.CREATE);
await fileIo.write(file.fd, this.imageBuffer);
await fileIo.close(file.fd);
// 生成应用URI
const uri = fileUri.getUriFromPath(path);
const data: systemShare.SharedData = new systemShare.SharedData({
utd: utd.UniformDataType.IMAGE,
uri: uri
});
const controller: systemShare.ShareController = new systemShare.ShareController(data);
controller.show(this.context, {
previewMode: systemShare.SharePreviewMode.DETAIL,
selectionMode: systemShare.SelectionMode.SINGLE
}).then(() => {
console.info('ShareController show success.');
}).catch((error: BusinessError) => {
console.error(`ShareController show error. code: ${error?.code}, message: ${error?.message}`);
});
} catch (error) {
console.error(`fileIo error. Code: ${error?.code}, message: ${error?.message}`);
}
}
// Endsolution4
aboutToAppear(): void {
const filesDir = this.context.filesDir;
try {
[
[this.context.resourceManager.getMediaContentSync($r('app.media.exampleImage').id), '/exampleImage.jpg'],
[this.context.resourceManager.getMediaContentSync($r('app.media.exampleVideo').id), '/exampleVideo.mp4'],
[this.context.resourceManager.getMediaContentSync($r('app.media.startIcon').id), '/icon.jpg'],
[this.context.resourceManager.getMediaContentSync($r('app.media.exampleKnock1').id), '/exampleKnock1.jpg'],
[this.context.resourceManager.getMediaContentSync($r('app.media.exampleKnock2').id), '/exampleKnock2.png'],
[this.context.resourceManager.getMediaContentSync($r('app.media.exampleKnock3').id), '/exampleKnock3.jpg'],
].forEach(item => {
let file = fileIo.openSync(filesDir + item[1], fileIo.OpenMode.READ_WRITE | fileIo.OpenMode.CREATE);
let writeLen = fileIo.writeSync(file.fd, (item[0] as Uint8Array).buffer);
console.info(`write data to file succeed and size is: ${writeLen}`);
fileIo.closeSync(file);
});
} catch (error) {
console.error(`getMediaContentSync error. Code: ${error?.code}, message: ${error?.message}`);
}
}
private async shareCustomPanel() {
let filePath = this.context.filesDir + '/exampleImage.jpg';
let utdTypeId = utd.getUniformDataTypeByFilenameExtension('.jpg', utd.UniformDataType.IMAGE);
let shareData: systemShare.SharedData = new systemShare.SharedData({
utd: utdTypeId,
uri: fileUri.getUriFromPath(filePath),
title: 'Picture Title',
description: 'Picture Description',
});
// Startsolution2
let controller: systemShare.ShareController = new systemShare.ShareController(shareData);
controller.show(this.context, {
selectionMode: systemShare.SelectionMode.SINGLE,
previewMode: systemShare.SharePreviewMode.DETAIL,
excludedAbilities: [systemShare.ShareAbilityType.PRINT, systemShare.ShareAbilityType.COPY_TO_PASTEBOARD,
systemShare.ShareAbilityType.SAVE_AS_FILE, systemShare.ShareAbilityType.SAVE_TO_MEDIA_ASSET]
}).then(() => {
console.info('ShareController show success.');
}).catch((error: BusinessError) => {
console.error(`ShareController show error. code: ${error?.code}, message: ${error?.message}`);
});
// Endsolution2
}
} 发送方需使用精确UTD,图片优先使用general.image或按扩展名解析出的UTD,不要直接传应用沙箱物理路径。若需实现文字和图片一起分享,可通过多条SharedRecord添加文本和图片记录,两端需约定清楚数据结构。如需定向跳转到指定应用的指定编辑页,优先让目标应用提供明确的分享接收Ability或App Linking/深链能力。
Q:使用Share Kit API分享图片,URI是本地图片地址,如何设置为http或者https开头的线上图片?
A:系统分享不支持分享远端图片;应先将网络图片保存至本地,获取路径后再进行分享。
Q:调用系统分享Share Kit后,没有分享的目标应用。
A:目标应用需要具备数据处理能力组件并在配置文件中声明所支持的分享能力,可参考目标应用处理分享内容。
Q:使用systemShare.getSharedData(want)获取分享的数据,报错1003703001,如何解决?
A:数据记录数量超过限制,可以不通过getSharedData接口获取分享数据,直接从want.uri属性,即分享的链接中,解析出分享的数据。
Q:Share Kit简介文档中"宿主应用配置分享数据描述信息总量不能超过200KB"是指分享的文件大小吗?
A:Share Kit简介文档中提到的200KB限制是指分享的数据描述信息的大小限制,而不是单个文件的大小限制。即通过Share Kit分享多个文件或大量数据时,需确保这些数据的总描述信息不超过200KB的限制。
Q:PC端用华为分享发一个文件到PAD端,PAD端接收后会自动打开文件预览,但是PAD端华为分享发给PC端一个文件,PC接收后显示的文件分享列表,并没有文件预览,这是系统规格吗?
A:是的,系统规格;其他设备将文件分享至PC时,文件默认保存至华为分享目录,不会直接打开;当分享到PAD上会直接预览打开。
Q:接入华为分享是否还需要再对接其他系统(如微信、微博等)的分享?
A:统一分享提供分享入口和面板,目标应用需要对能够接收的数据类型进行定义,宿主应用需要按照规范定义分享数据。接入统一分享后,系统自动识别该分享数据可分享至哪些目标应用,在分享面板中展示。无需开发者进行主动接入。具体流程可参考系统分享流程。系统分享的实现宿主应用和目标应用要分别遵守宿主应用开发规范和目标应用开发规范。
Q:使用系统的分享,怎么拿到结果回调。
A:可参考获取分享结果,注意从5.1.0(18)版本开始,支持宿主应用获取用户分享结果。
Q:系统分享"保存至图库"是否需要权限,按钮能否自定义?
A:系统分享保存图片到图库不需要额外申请权限,保证用户体验的一致性,无法自定义面板上的公共按钮。
Q:碰一碰分享是否可以跳过卡片分享页面直接分享数据?
A:碰一碰不支持直接分享数据,存在误触的可能性,极端情况会导致数据发送给非预期的设备。
Q:手机分享文件给PC设备,是否可以默认指定应用打开文件?
A:目前不支持,PC接收到文件后会存储在文件管理"华为分享"目录,默认用文件管理高亮显示接收到的文件。
Q:系统分享如何传递自定义参数?
A:可参考SharedData接口说明。
Q:如何区分通过隔空传送分享过来的数据和通过本地文件管理点击文件选择其他应用打开的数据?
A:隔空传送接收的数据不会直接发给应用,通常通过图库或者文件管理接收,最常用的是通过预览的方式拉起应用。
Q:如何实现自定义分享方式?
A:可参考半接模式分享,开发者自行开发分享能力面板,并在分享面板中提供系统分享入口。适用于分享方式区有商业诉求,或有自己独有的业务逻辑的开发者。
Q:分享PDF文件时,分享面板没有自己开发的应用,如何打开三方应用分享的PDF文件?
A:具体操作步骤如下:
{
"module": {
"name": "entry",
"type": "entry",
"description": "$string:module_desc",
"mainElement": "EntryAbility",
"deviceTypes": [
"phone"
],
"deliveryWithInstall": true,
"installationFree": false,
"pages": "$profile:main_pages",
"abilities": [
{
"name": "EntryAbility",
"srcEntry": "./ets/entryability/EntryAbility.ets",
"description": "$string:EntryAbility_desc",
"icon": "$media:layered_image",
"label": "$string:EntryAbility_label",
"startWindowIcon": "$media:startIcon",
"startWindowBackground": "$color:start_window_background",
"exported": true,
"skills": [
{
"entities": [
"entity.system.home"
],
"actions": [
"ohos.want.action.home"
]
},
// 添加skill配置
{
"actions": [
// 必需,声明数据处理能力
"ohos.want.action.viewData",
"ohos.want.action.sendData"
],
"uris": [
{
"scheme": "file",
// 目标应用在配置支持接收的数据类型时,需穷举支持的UTD。
"utd": "com.adobe.pdf",
// maxFileSupported对于归属指定类型的文件,标识一次支持接收的最大数量。默认为0,代表不支持此类文件的分享。
"maxFileSupported": 1
},
{
"scheme": "file",
"utd": "general.object",
"maxFileSupported": 1
}
]
}
]
}
]
}
} import { UIAbility, Want } from '@kit.AbilityKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { window } from '@kit.ArkUI';
import { systemShare } from '@kit.ShareKit';
import { fileIo } from '@kit.CoreFileKit';
import { BusinessError } from '@kit.BasicServicesKit';
const DOMAIN = 0x0000;
export default class EntryAbility extends UIAbility {
onCreate(want: Want): void {
hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onCreate');
this.handleSharedData(want);
}
onNewWant(want: Want): void {
this.handleSharedData(want);
}
onWindowStageCreate(windowStage: window.WindowStage): void {
hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onWindowStageCreate');
windowStage.loadContent('pages/Index', (err) => {
if (err.code) {
hilog.error(DOMAIN, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err));
return;
}
hilog.info(DOMAIN, 'testTag', 'Succeeded in loading the content.');
});
}
handleSharedData(want: Want) {
systemShare.getSharedData(want)
.then((data: systemShare.SharedData) => {
data?.getRecords()?.forEach((record: systemShare.SharedRecord) => {
if (!record.uri) {
return;
}
try {
// 获取uri,缓存至应用沙箱
const file = fileIo.openSync(record.uri, fileIo.OpenMode.READ_ONLY);
const destPath = this.context.cacheDir + '/' + file.name;
const outFile = fileIo.openSync(destPath, fileIo.OpenMode.READ_WRITE | fileIo.OpenMode.CREATE);
const shareFilePath = outFile.path;
fileIo.copyFileSync(file.fd, outFile.fd);
fileIo.closeSync(file);
fileIo.closeSync(outFile);
AppStorage.setOrCreate('SHARE_FILE_PATH', shareFilePath);
} catch (error) {
console.error(`Failed to parse share data. Code: ${error.code}, message: ${error.message}`);
}
});
})
.catch((error: BusinessError) => {
console.error(`Failed to getSharedData. Code: ${error.code}, message: ${error.message}`);
});
}
}; import { pdfService, PdfView, pdfViewManager } from '@kit.PDFKit';
@Entry
@Component
struct Index {
context = this.getUIContext().getHostContext()!;
private controller: pdfViewManager.PdfController = new pdfViewManager.PdfController();
build() {
Column() {
Button('预览')
.onClick(() => {
const path = AppStorage.get('SHARE_FILE_PATH') as string;
const pdfDocument: pdfService.PdfDocument = new pdfService.PdfDocument();
pdfDocument.loadDocument(path);
this.controller.loadDocument(path);
})
PdfView({
controller: this.controller,
pageFit: pdfService.PageFit.FIT_WIDTH,
showScroll: true
})
.id('pdfview_app_view')
.layoutWeight(1)
}
.height('100%')
.width('100%')
}
} Q:为什么WPS移动版分享PDF文件看不到目标应用?
A:宿主应用在发起文件分享时,填写的UTD可能有差异,目标应用需穷举支持的UTD类型“general.object”。