文档管理中心
指南应用框架ArkData(方舟数据管理)标准化数据定义标准化数据结构 (C/C++)

标准化数据结构 (C/C++)

场景介绍

针对utd.h中定义的部分常见UTD标准化数据类型,为了方便业务使用,提供了标准化数据结构。例如,系统定义的桌面图标类型(标准化数据类型标识为'OH_UdsAppItem')明确定义了相关描述信息。

某些业务场景下应用可以直接使用我们具体定义的UTD标准化数据结构,例如跨应用拖拽场景。拖出方应用可以按照标准化数据结构将拖拽数据写入绑定拖拽事件,拖入方应用从拖拽事件中读取拖拽数据并按照标准化数据结构进行数据的解析。这使得不同应用间的数据交互遵从相同的标准定义,有效减少了跨应用数据交互的开发工作量。

基本概念

  • 标准化数据结构:Unified Data Structure,简称UDS。主要针对部分标准化数据类型定义了统一的数据内容结构,并明确了对应的描述信息。应用间使用标准化数据结构进行数据交互后,将遵从统一的解析标准,可有效减少适配相关的工作量。一般用于跨应用跨设备间的数据交互,比如拖拽。

接口说明

详细的接口说明请参考uds.h中定义的标准化数据结构相关接口。

展开
接口名称 描述
OH_UdmfData* OH_UdmfData_Create() 创建统一数据对象指针及实例对象
OH_UdmfRecord* OH_UdmfRecord_Create() 创建统一数据记录指针及实例对象
OH_UdsPlainText* OH_UdsPlainText_Create() 创建纯文本类型指针及实例对象
int OH_UdmfRecord_GetPlainText(OH_UdmfRecord* pThis, OH_UdsPlainText* plainText) 从统一数据记录中获取纯文本类型
int OH_UdsPlainText_SetContent(OH_UdsPlainText* pThis, const char* content) 设置纯文本类型中的纯文本内容参数
int OH_UdmfRecord_AddPlainText(OH_UdmfRecord* pThis, OH_UdsPlainText* plainText) 添加纯文本类型数据至统一数据记录中
int OH_UdmfData_AddRecord(OH_UdmfData* pThis, OH_UdmfRecord* record) 添加一个数据记录到统一数据对象中
OH_UdsFileUri* OH_UdsFileUri_Create() 创建文件Uri类型指针及实例对象
int OH_UdsFileUri_SetFileUri(OH_UdsFileUri* pThis, const char* fileUri) 设置文件Uri类型对象的Uri信息
int OH_UdsFileUri_SetFileType(OH_UdsFileUri* pThis, const char* fileType) 设置文件Uri类型对象的文件类型
int OH_UdmfRecord_AddFileUri(OH_UdmfRecord* pThis, OH_UdsFileUri* fileUri) 增加文件Uri类型数据至统一数据记录中
int OH_Udmf_SetUnifiedData(Udmf_Intention intention, OH_UdmfData* unifiedData,char* key, unsigned int keyLen) 向统一数据管理框架数据库中写入统一数据对象数据
void OH_UdsPlainText_Destroy(OH_UdsPlainText* pThis) 销毁纯文本类型数据指针指向的实例对象
void OH_UdmfData_Destroy(OH_UdmfData* pThis) 销毁统一数据对象指针指向的实例对象
void OH_UdsFileUri_Destroy(OH_UdsFileUri* pThis) 销毁文件Uri类型的实例对象

添加动态链接库

CMakeLists.txt中添加以下库。

收起
自动换行
深色代码主题
复制
  1. libudmf.so, libhilog_ndk.z.so

引用头文件

收起
自动换行
深色代码主题
复制
  1. #include <database/udmf/uds.h>
  2. #include <database/udmf/udmf.h>
  3. #include <database/udmf/udmf_meta.h>
  4. #include <database/udmf/udmf_err_code.h>
  5. #include <hilog/log.h>
  6. #undef LOG_TAG
  7. #define LOG_TAG "MY_LOG"

纯文本类型数据结构的使用

  1. 创建PlainText对象指针。
  2. 添加PlainText内容。
  3. 获取数据。
  4. 使用完成后销毁指针。
收起
自动换行
深色代码主题
复制
  1. // 1.创建PlainText对象指针
  2. OH_UdmfRecord *plainTextRecord = OH_UdmfRecord_Create();
  3. if (plainTextRecord == nullptr) {
  4. return Udmf_ErrCode::UDMF_ERR;
  5. }
  6. OH_UdsPlainText *plainText = OH_UdsPlainText_Create();
  7. if (plainText == nullptr) {
  8. OH_UdmfRecord_Destroy(plainTextRecord);
  9. return Udmf_ErrCode::UDMF_ERR;
  10. }
  11. char content[] = "hello world";
  12. // 2.添加PlainText内容
  13. int32_t ret = OH_UdsPlainText_SetContent(plainText, content);
  14. if (ret != Udmf_ErrCode::UDMF_E_OK) {
  15. OH_LOG_ERROR(LOG_APP, "OH_UdsPlainText_SetContent error!");
  16. OH_UdsPlainText_Destroy(plainText);
  17. OH_UdmfRecord_Destroy(plainTextRecord);
  18. return ret;
  19. }
  20. ret = OH_UdmfRecord_AddPlainText(plainTextRecord, plainText);
  21. if (ret != Udmf_ErrCode::UDMF_E_OK) {
  22. OH_LOG_ERROR(LOG_APP, "OH_UdmfRecord_AddPlainText error!");
  23. OH_UdsPlainText_Destroy(plainText);
  24. OH_UdmfRecord_Destroy(plainTextRecord);
  25. return ret;
  26. }
  27. // 3.获取PlainText数据
  28. OH_UdsPlainText *plainText2 = OH_UdsPlainText_Create();
  29. ret = OH_UdmfRecord_GetPlainText(plainTextRecord, plainText2);
  30. if (ret != Udmf_ErrCode::UDMF_E_OK) {
  31. OH_LOG_ERROR(LOG_APP, "OH_UdmfRecord_GetPlainText error!");
  32. OH_UdsPlainText_Destroy(plainText);
  33. OH_UdmfRecord_Destroy(plainTextRecord);
  34. OH_UdsPlainText_Destroy(plainText2);
  35. return ret;
  36. }
  37. const char *content2 = OH_UdsPlainText_GetContent(plainText2);
  38. if (content2 != nullptr) {
  39. OH_LOG_INFO(LOG_APP, "content = %{public}s.", content2);
  40. }
  41. // 4.使用完成后销毁指针。
  42. OH_UdsPlainText_Destroy(plainText);
  43. OH_UdmfRecord_Destroy(plainTextRecord);
  44. OH_UdsPlainText_Destroy(plainText2);

fileUri类型的数据结构的使用

  1. 创建fileUri类型的数据结构。
  2. 设置fileUri中的URL和描述信息。
  3. 创建OH_UdmfRecord对象,并向OH_UdmfRecord中添加fileUri类型数据。
  4. 获取fileUri数据。
  5. 使用完成后销毁指针。
收起
自动换行
深色代码主题
复制
  1. // 1.创建fileUri类型的数据结构
  2. const char *uri = "https://xxx/xx/xx.jpg";
  3. OH_UdsFileUri *fileUri = OH_UdsFileUri_Create();
  4. if (fileUri == nullptr) {
  5. return Udmf_ErrCode::UDMF_ERR;
  6. }
  7. // 2. 设置fileUri中的URL和文件类型信息。
  8. int32_t ret = OH_UdsFileUri_SetFileUri(fileUri, uri);
  9. if (ret != Udmf_ErrCode::UDMF_E_OK) {
  10. OH_UdsFileUri_Destroy(fileUri);
  11. return ret;
  12. }
  13. ret = OH_UdsFileUri_SetFileType(fileUri, UDMF_META_IMAGE);
  14. if (ret != Udmf_ErrCode::UDMF_E_OK) {
  15. OH_UdsFileUri_Destroy(fileUri);
  16. return ret;
  17. }
  18. // 3. 创建OH_UdmfRecord对象,并向OH_UdmfRecord中添加fileUri类型数据。
  19. OH_UdmfRecord *record = OH_UdmfRecord_Create();
  20. if (record == nullptr) {
  21. OH_UdsFileUri_Destroy(fileUri);
  22. return Udmf_ErrCode::UDMF_ERR;
  23. }
  24. ret = OH_UdmfRecord_AddFileUri(record, fileUri);
  25. if (ret != Udmf_ErrCode::UDMF_E_OK) {
  26. OH_UdsFileUri_Destroy(fileUri);
  27. OH_UdmfRecord_Destroy(record);
  28. return ret;
  29. }
  30. // 4. 获取fileUri数据。
  31. OH_UdsFileUri *fileUri1 = OH_UdsFileUri_Create();
  32. if (fileUri1 == nullptr) {
  33. OH_UdsFileUri_Destroy(fileUri);
  34. OH_UdmfRecord_Destroy(record);
  35. return Udmf_ErrCode::UDMF_ERR;
  36. }
  37. ret = OH_UdmfRecord_GetFileUri(record, fileUri1);
  38. if (ret != Udmf_ErrCode::UDMF_E_OK) {
  39. OH_UdsFileUri_Destroy(fileUri);
  40. OH_UdmfRecord_Destroy(record);
  41. OH_UdsFileUri_Destroy(fileUri1);
  42. return ret;
  43. }
  44. const char *fileUriStr = OH_UdsFileUri_GetFileUri(fileUri1);
  45. if (fileUriStr != nullptr) {
  46. OH_LOG_INFO(LOG_APP, "fileUri1 = %{public}s.", fileUriStr);
  47. }
  48. // 5. 使用完成后销毁指针。
  49. OH_UdsFileUri_Destroy(fileUri);
  50. OH_UdmfRecord_Destroy(record);
  51. OH_UdsFileUri_Destroy(fileUri1);
在 指南 中进行搜索
请输入您想要搜索的关键词