openharmony 鸿蒙 asset-native-add

2025-06-12 浏览 (1)

Adding an Asset (C/C++)

Available APIs

You can use OH_Asset_Add to add an asset.

The following table describes the attributes for adding an asset.

NOTE

In the following table, the attributes starting with ASSET_TAG_DATA_LABEL are custom asset attributes reserved. These attributes are not encrypted. Therefore, do not put personal data in these attributes.

Attribute Name (Asset_Tag)Attribute Content (Asset_Value)MandatoryDescription
ASSET_TAG_SECRETType: uint8[]
Length: 1-1024 bytes
YesAsset in plaintext.
ASSET_TAG_ALIASType: uint8[]
Length: 1-256 bytes
YesAsset alias, which uniquely identifies an asset.
ASSET_TAG_ACCESSIBILITYType: uint32_t
Value range: see Asset_Accessibility
NoAccess control based on the lock screen status.
ASSET_TAG_REQUIRE_PASSWORD_SETType: boolNoWhether the asset is accessible only when a lock screen password is set.
ASSET_TAG_AUTH_TYPEType: uint32_t
Value range: see Asset_AuthType
NoType of user authentication required for accessing the asset.
ASSET_TAG_SYNC_TYPEType: uint32_t
Value range: see Asset_SyncType
NoType of sync supported by the asset.
ASSET_TAG_IS_PERSISTENTType: boolNoWhether to retain the asset when the application is uninstalled.
NOTE: If this parameter is set, the application must apply for the ohos.permission.STORE_PERSISTENT_DATA permission.
ASSET_TAG_DATA_LABEL_CRITICAL_1Type: uint8[]
Length: 1-2048 bytes
NoAsset attribute information customized by the service with integrity protection.
NOTE: The data length is 1 to 512 bytes before API version 12.
ASSET_TAG_DATA_LABEL_CRITICAL_2Type: uint8[]
Length: 1-2048 bytes
NoAsset attribute information customized by the service with integrity protection.
NOTE: The data length is 1 to 512 bytes before API version 12.
ASSET_TAG_DATA_LABEL_CRITICAL_3Type: uint8[]
Length: 1-2048 bytes
NoAsset attribute information customized by the service with integrity protection.
NOTE: The data length is 1 to 512 bytes before API version 12.
ASSET_TAG_DATA_LABEL_CRITICAL_4Type: uint8[]
Length: 1-2048 bytes
NoAsset attribute information customized by the service with integrity protection.
NOTE: The data length is 1 to 512 bytes before API version 12.
ASSET_TAG_DATA_LABEL_NORMAL_1Type: uint8[]
Length: 1-2048 bytes
NoAsset attribute information customized by the service without integrity protection.
NOTE: The data length is 1 to 512 bytes before API version 12.
ASSET_TAG_DATA_LABEL_NORMAL_2Type: uint8[]
Length: 1-2048 bytes
NoAsset attribute information customized by the service without integrity protection.
NOTE: The data length is 1 to 512 bytes before API version 12.
ASSET_TAG_DATA_LABEL_NORMAL_3Type: uint8[]
Length: 1-2048 bytes
NoAsset attribute information customized by the service without integrity protection.
NOTE: The data length is 1 to 512 bytes before API version 12.
ASSET_TAG_DATA_LABEL_NORMAL_4Type: uint8[]
Length: 1-2048 bytes
NoAsset attribute information customized by the service without integrity protection.
NOTE: The data length is 1 to 512 bytes before API version 12.
ASSET_TAG_DATA_LABEL_NORMAL_LOCAL_112+Type: uint8[]
Length: 1-2048 bytes
NoLocal attribute information about the asset. The value is assigned by the service without integrity protection and will not be synced.
ASSET_TAG_DATA_LABEL_NORMAL_LOCAL_212+Type: uint8[]
Length: 1-2048 bytes
NoLocal attribute information about the asset. The value is assigned by the service without integrity protection and will not be synced.
ASSET_TAG_DATA_LABEL_NORMAL_LOCAL_312+Type: uint8[]
Length: 1-2048 bytes
NoLocal attribute information about the asset. The value is assigned by the service without integrity protection and will not be synced.
ASSET_TAG_DATA_LABEL_NORMAL_LOCAL_412+Type: uint8[]
Length: 1-2048 bytes
NoLocal attribute information about the asset. The value is assigned by the service without integrity protection and will not be synced.
ASSET_TAG_CONFLICT_RESOLUTIONType: uint32_t
Value range: see Asset_ConflictResolution
NoPolicy for resolving the conflict (for example, duplicate alias).
ASSET_TAG_REQUIRE_ATTR_ENCRYPTED14+Type: boolNoWhether to encrypt the customized asset attribute information. By default, the information does not need to be encrypted.
ASSET_TAG_GROUP_ID18+Type: Uint8[]
Length: 7-127 bytes
NoGroup to which the asset to be added belongs. By default, this parameter is not specified.

Constraints

  • Alias-based access

    Assets are stored in the ASSET database in ciphertext and uniquely identified by the service identity and alias. The alias of each asset must be unique.

  • Custom service data storage

    ASSET provides 12 custom asset attributes starting with ASSET_TAG_DATA_LABEL for services. If the 12 custom attributes are used, you can combine multiple data segments in a certain format (for example, JSON) into an ASSET attribute.

    ASSET protects the integrity of the attributes starting with ASSET_TAG_DATA_LABEL_CRITICAL. These attributes cannot be changed once written.

Example

Add an asset that is accessible when the user unlocks the device for the first time. The asset includes password demo_pwd, alias demo_alias, and additional information demo_label.

For details about how to add an asset to a group, see Adding an Asset to a Group.

  1. Add the dynamic library in the CMake script.

    target_link_libraries(entry PUBLIC libasset_ndk.z.so)
    
  2. Add an asset.

    #include <string.h>
    
    #include "asset/asset_api.h"
    
    void AddAsset() {
       static const char *SECRET = "demo_pwd";
       static const char *ALIAS = "demo_alias";
       static const char *LABEL = "demo_label";
    
       Asset_Blob secret = { (uint32_t)(strlen(SECRET)), (uint8_t *)SECRET };
       Asset_Blob alias = { (uint32_t)(strlen(ALIAS)), (uint8_t *)ALIAS };
       Asset_Blob label = { (uint32_t)(strlen(LABEL)), (uint8_t *)LABEL };
       Asset_Attr attr[] = {
          { .tag = ASSET_TAG_ACCESSIBILITY, .value.u32 = ASSET_ACCESSIBILITY_DEVICE_FIRST_UNLOCKED },
          { .tag = ASSET_TAG_SECRET, .value.blob = secret },
          { .tag = ASSET_TAG_ALIAS, .value.blob = alias },
          { .tag = ASSET_TAG_DATA_LABEL_NORMAL_1, .value.blob = label },
       };
    
       int32_t ret = OH_Asset_Add(attr, sizeof(attr) / sizeof(attr[0]));
       if (ret == ASSET_SUCCESS) {
          // Asset added successfully.
       } else {
          // Failed to add Asset.
       }
    }
    

你可能感兴趣的鸿蒙文章

harmony 鸿蒙Asset Store Kit (Asset Store Service)

harmony 鸿蒙Performing Asset Operations in Specified User Space (for System Applications Only)

harmony 鸿蒙Adding an Asset (ArkTS)

harmony 鸿蒙Managing Assets in a Group

harmony 鸿蒙Querying an Asset with User Authentication (ArkTS)

harmony 鸿蒙Querying Assets (ArkTS)

harmony 鸿蒙Removing Assets (ArkTS)

harmony 鸿蒙Updating an Asset (ArkTS)

harmony 鸿蒙Managing Assets in a Group

harmony 鸿蒙Querying Assets (C/C++)

  • 所属分类: 后端技术
  • 本文标签: 软件 鸿蒙
  • 版权声明: 本文链接 https://seaxiang.com/blog/YvgzPb