• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# 标准化数据结构 (C/C++)
2<!--Kit: ArkData-->
3<!--Subsystem: DistributedDataManager-->
4<!--Owner: @jcwen-->
5<!--Designer: @junathuawei1; @zph000-->
6<!--Tester: @lj_liujing; @yippo; @logic42-->
7<!--Adviser: @ge-yafang-->
8
9
10## 场景介绍
11
12针对[UTD标准化数据类型](../reference/apis-arkdata/capi-utd-h.md)中的部分常见类型,为了方便业务使用,提供了标准化数据结构。例如,系统定义的桌面图标类型(标准化数据类型标识为'OH_UdsAppItem')明确定义了相关描述信息。
13
14某些业务场景下应用可以直接使用我们具体定义的UTD标准化数据结构,例如跨应用拖拽场景。拖出方应用可以按照标准化数据结构将拖拽数据写入[拖拽事件](../ui/ndk-drag-event.md),拖入方应用从拖拽事件中读取拖拽数据并按照标准化数据结构进行数据的解析。这使得不同应用间的数据交互遵从相同的标准定义,有效减少了跨应用数据交互的开发工作量。
15
16## 基本概念
17
18- **标准化数据结构**:Unified Data Structure,简称UDS。主要针对部分标准化数据类型定义了统一的数据内容结构,并明确了对应的描述信息。应用间使用标准化数据结构进行数据交互后,将遵从统一的解析标准,可有效减少适配相关的工作量。一般用于跨应用跨设备间的数据交互,比如拖拽。
19
20## 接口说明
21
22详细的接口说明请参考[标准化数据结构相关接口](../reference/apis-arkdata/capi-uds-h.md)。
23
24| 接口名称                                                                                    | 描述                                          |
25|-----------------------------------------------------------------------------------------|---------------------------------------------|
26| OH_UdmfData* OH_UdmfData_Create()                                                       | 创建统一数据对象指针及实例对象 |
27| OH_UdmfRecord* OH_UdmfRecord_Create()                                                   | 创建统一数据记录指针及实例对象 |
28| OH_UdsPlainText* OH_UdsPlainText_Create()                                               | 创建纯文本类型指针及实例对象 |
29| int OH_UdmfRecord_GetPlainText(OH_UdmfRecord* pThis, OH_UdsPlainText* plainText)        | 从统一数据记录中获取纯文本类型     |
30| int OH_UdsPlainText_SetContent(OH_UdsPlainText* pThis, const char* content)             | 设置纯文本类型中的纯文本内容参数  |
31| int OH_UdmfRecord_AddPlainText(OH_UdmfRecord* pThis, OH_UdsPlainText* plainText)        | 添加纯文本类型数据至统一数据记录中 |
32| int OH_UdmfData_AddRecord(OH_UdmfData* pThis, OH_UdmfRecord* record)                    | 添加一个数据记录到统一数据对象中   |
33| OH_UdsFileUri* OH_UdsFileUri_Create()                                                   | 创建文件Uri类型指针及实例对象 |
34| int OH_UdsFileUri_SetFileUri(OH_UdsFileUri* pThis, const char* fileUri)                 | 设置文件Uri类型对象的Uri信息 |
35| int OH_UdsFileUri_SetFileType(OH_UdsFileUri* pThis, const char* fileType)               | 设置文件Uri类型对象的文件类型 |
36| int OH_UdmfRecord_AddFileUri(OH_UdmfRecord* pThis, OH_UdsFileUri* fileUri)              | 增加文件Uri类型数据至统一数据记录中 |
37| int OH_Udmf_SetUnifiedData(Udmf_Intention intention, OH_UdmfData* unifiedData,char* key, unsigned int keyLen) | 从统一数据管理框架数据库中写入统一数据对象数据 |
38| void OH_UdsPlainText_Destroy(OH_UdsPlainText* pThis)                                    | 销毁纯文本类型数据指针指向的实例对象 |
39| void OH_UdmfData_Destroy(OH_UdmfData* pThis)                                            | 销毁统一数据对象指针指向的实例对象 |
40| void OH_UdsFileUri_Destroy(OH_UdsFileUri* pThis)                                        | 销毁文件Uri类型的实例对象 |
41
42## 添加动态链接库
43
44CMakeLists.txt中添加以下库。
45
46```txt
47libudmf.so, libhilog_ndk.z.so
48```
49
50## 引用头文件
51
52```c
53#include <cstdio>
54#include <cstring>
55#include <database/udmf/utd.h>
56#include <database/udmf/uds.h>
57#include <database/udmf/udmf.h>
58#include <database/udmf/udmf_meta.h>
59#include <database/udmf/udmf_err_code.h>
60#include <hilog/log.h>
61
62#undef LOG_TAG
63#define LOG_TAG "MY_LOG"
64```
65
66## 纯文本类型数据结构的使用
67
681. 创建PlainText对象指针。
692. 添加PlainText内容。
703. 获取数据。
714. 使用完成后销毁指针。
72
73```c
74// 1.创建PlainText对象指针
75OH_UdmfRecord *plainTextRecord = OH_UdmfRecord_Create();
76OH_UdsPlainText *plainText = OH_UdsPlainText_Create();
77char content[] = "hello world";
78
79// 2.添加PlainText内容
80OH_UdsPlainText_SetContent(plainText, content);
81OH_UdmfRecord_AddPlainText(plainTextRecord, plainText);
82
83// 3.获取PlainText数据
84OH_UdsPlainText* plainText2 = OH_UdsPlainText_Create();
85OH_UdmfRecord_GetPlainText(plainTextRecord, plainText2);
86const char* content2 = OH_UdsPlainText_GetContent(plainText2);
87
88OH_LOG_INFO(LOG_APP, "content = %{public}s.", content2);
89// 4.使用完成后销毁指针。
90OH_UdsPlainText_Destroy(plainText);
91OH_UdmfRecord_Destroy(plainTextRecord);
92OH_UdsPlainText_Destroy(plainText2);
93```
94
95## fileUri类型的数据结构的使用
96
971. 创建fileUri类型的数据结构。
982. 设置fileUri中的URL和描述信息。
993. 创建OH_UdmfRecord对象,并向OH_UdmfRecord中添加fileUri类型数据。
1004. 获取fileUri数据。
1015. 使用完成后销毁指针。
102
103```c
104// 1.创建fileUri类型的数据结构
105const char* uri = "https://xxx/xx/xx.jpg";
106OH_UdsFileUri* fileUri = OH_UdsFileUri_Create();
107// 2. 设置fileUri中的URL和描述信息。
108OH_UdsFileUri_SetFileUri(fileUri, uri);
109OH_UdsFileUri_SetFileType(fileUri, UDMF_META_IMAGE);
110// 3. 创建OH_UdmfRecord对象,并向OH_UdmfRecord中添加fileUri类型数据。
111OH_UdmfRecord* record = OH_UdmfRecord_Create();
112OH_UdmfRecord_AddFileUri(record, fileUri);
113// 4. 获取fileUri数据。
114OH_UdsFileUri* fileUri1 = OH_UdsFileUri_Create();
115OH_UdmfRecord_GetFileUri(record, fileUri1);
116const char* fileUriStr = OH_UdsFileUri_GetFileUri(fileUri1);
117OH_LOG_INFO(LOG_APP, "fileUri1 = %{public}s.", fileUriStr);
118// 5. 使用完成后销毁指针。
119OH_UdsFileUri_Destroy(fileUri);
120OH_UdmfRecord_Destroy(record);
121OH_UdsFileUri_Destroy(fileUri1);
122```