
回复
鸿蒙Next的UTD(Uniform Type Descriptor)通过标准化数据类型,解决跨应用、跨设备的数据识别难题。本文用精简语言解析核心概念与实战要点~
general.image
),避免类型歧义media > image > photo
)属性 | 说明 | 示例 |
---|---|---|
typeId | 唯一标识符 | com.example.custom.pdf |
belongingTo | 父类型(可多级) | general.document |
description | 类型描述 | “自定义PDF文档” |
fileExtensions | 关联文件后缀 | [“.pdf”, “.pdfx”] |
mimeType | MIME类型 | “application/pdf” |
media
├─ image (general.image)
│ ├─ photo (image.photo)
│ └─ graphic (image.graphic)
└─ video (general.video)
└─ movie (video.movie)
general.image
的应用(如相册、编辑器)video.movie
类型视频
typeId: 'com.example.app.custom_audio',
belongingTo: ['general.audio'], // 继承自音频类型
description: '自定义无损音频',
fileExtensions: ['.lossless'],
mimeType: 'audio/x-lossless'
action: 'android.intent.action.SEND',
type: customType.mimeType,
extra: { 'android.intent.extra.STREAM': data
## 四、兼容性设计:让自定义类型「融入系统生态」🌐
### 1. 父子类型兼容
```typescript
// 检查是否属于某父类型
TypeDescriptor.isAncestor('general.image', 'image.photo'); // 返回true
// 传输时自动转换为通用类型
if (device.type === 'wearable') {
const fallbackType = TypeDescriptor.getFallbackType('com.example.custom_audio');
sendDataWithType(fallbackType); // 自动降级为general.audio
}
场景 | 解决方案 |
---|---|
第三方应用定义同名类型 | 优先使用系统预置类型,自定义类型需声明priority |
类型属性变更 | 通过TypeDescriptor.update() 发布新版本 |
const cadType = new TypeDescriptor({
typeId: 'com.engineer.app.cad_draw',
belongingTo: ['general.document', 'engineering'], // 多重父类型
description: 'CAD工程图纸',
fileExtensions: ['.dwg', '.dxf'],
mimeType: 'application/cad'
});
// CAD编辑器发送图纸
const drawing = new File('project.dwg', cadType.typeId);
ability.startAbility({
action: 'com.engineer.app.ACTION_OPEN_CAD',
type: cadType.mimeType,
extra: { 'cad_data': drawing }
});
// 审批应用接收并预览
if (TypeDescriptor.isCompatible(want.type, cadType.typeId)) {
const renderer = new CadRenderer(want.extra['cad_data']);
renderer.preview();
}
general.image
),避免重复造轮子engineering.cad
属于general.document
)