The UTD type indicates the data type of the content to be shared. The precise data type is essential for successful content sharing, allowing the host app to match the most suitable target app.
When constructing the data to be shared, the host app needs to specify the precise UTD type, which can be obtained in either of the following ways:
Obtaining the uniform data type ID based on the given file name extension and data type: For details, please refer to uniformTypeDescriptor.getUniformDataTypeByFilenameExtension.
import { common } from '@kit.AbilityKit';
import { BusinessError } from '@kit.BasicServicesKit';
import { uniformTypeDescriptor as utd } from '@kit.ArkData';
import { systemShare } from '@kit.ShareKit';
try {
let utdTypeId = utd.getUniformDataTypeByFilenameExtension('.jpg', utd.UniformDataType.IMAGE);
if (utdTypeId) {
// Construct ShareData. You need to configure a valid data record.
let shareData: systemShare.SharedData = new systemShare.SharedData({
utd: utdTypeId,
uri: 'file://.../xxx.jpg'
});
// Build ShareController.
let controller: systemShare.ShareController = new systemShare.ShareController(shareData);
// Obtain the UIAbility context object.
let uiContext: UIContext = this.getUIContext();
let context: common.UIAbilityContext = uiContext.getHostContext() as common.UIAbilityContext;
// Display the sharing panel.
controller.show(context, {
previewMode: systemShare.SharePreviewMode.DEFAULT,
selectionMode: systemShare.SelectionMode.SINGLE
});
}
} catch (e) {
let error: BusinessError = e as BusinessError;
console.error(`Failed to getUniformDataTypeByFilenameExtension. Code: ${error.code}, message: ${error.message} `);
} Obtaining the uniform data type ID based on the given MIME type and data type: For details, please refer to uniformTypeDescriptor.getUniformDataTypeByMIMEType.
import { common } from '@kit.AbilityKit';
import { BusinessError } from '@kit.BasicServicesKit';
import { uniformTypeDescriptor as utd } from '@kit.ArkData';
import { systemShare } from '@kit.ShareKit';
try {
let utdTypeId = utd.getUniformDataTypeByMIMEType('image/jpeg', utd.UniformDataType.IMAGE);
if (utdTypeId) {
// Construct ShareData. You need to configure a valid data record.
let shareData: systemShare.SharedData = new systemShare.SharedData({
utd: utdTypeId,
uri: 'file://.../xxx.jpg'
});
// Build ShareController.
let controller: systemShare.ShareController = new systemShare.ShareController(shareData);
// Obtain the UIAbility context object.
let uiContext: UIContext = this.getUIContext();
let context: common.UIAbilityContext = uiContext.getHostContext() as common.UIAbilityContext;
// Display the sharing panel.
controller.show(context, {
previewMode: systemShare.SharePreviewMode.DEFAULT,
selectionMode: systemShare.SelectionMode.SINGLE
});
}
} catch (e) {
let error: BusinessError = e as BusinessError;
console.error(`Failed to getUniformDataTypeByMIMEType. Code: ${error.code}, message: ${error.message} `);
}