openharmony 鸿蒙 asset-js-update

2025-06-12 浏览 (1)

Updating an Asset (ArkTS)

Available APIs

You can use update(query: AssetMap, attributesToUpdate: AssetMap), an asynchronous API, or updateSync(query: AssetMap, attributesToUpdate: AssetMap), a synchronous API, to update an asset.

The following table describes the attributes of AssetMap for updating an asset.

NOTE

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

  • Attributes of AssetMap in query:
Attribute Name (Tag)Attribute Content (Value)MandatoryDescription
ALIASType: Uint8Array
Length: 1-256 bytes
YesAsset alias, which uniquely identifies an asset.
ACCESSIBILITYType: number
Value range: see Accessibility
NoAccess control based on the lock screen status.
REQUIRE_PASSWORD_SETType: booleanNoWhether the asset is accessible only when a lock screen password is set.
AUTH_TYPEType: number
Value range: see AuthType
NoType of user authentication required for accessing the asset.
SYNC_TYPEType: number
Value range: see SyncType
NoAsset sync type.
IS_PERSISTENTType: booleanNoWhether to retain the asset when the application is uninstalled.
DATA_LABEL_CRITICAL_1Type: Uint8Array
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.
DATA_LABEL_CRITICAL_2Type: Uint8Array
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.
DATA_LABEL_CRITICAL_3Type: Uint8Array
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.
DATA_LABEL_CRITICAL_4Type: Uint8Array
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.
DATA_LABEL_NORMAL_1Type: Uint8Array
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.
DATA_LABEL_NORMAL_2Type: Uint8Array
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.
DATA_LABEL_NORMAL_3Type: Uint8Array
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.
DATA_LABEL_NORMAL_4Type: Uint8Array
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.
DATA_LABEL_NORMAL_LOCAL_112+Type: Uint8Array
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.
DATA_LABEL_NORMAL_LOCAL_212+Type: Uint8Array
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.
DATA_LABEL_NORMAL_LOCAL_312+Type: Uint8Array
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.
DATA_LABEL_NORMAL_LOCAL_412+Type: Uint8Array
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.
REQUIRE_ATTR_ENCRYPTED14+Type: booleanNoWhether to update the customized asset attribute information that is encrypted. By default, the unencrypted customized asset attribute information is updated.
GROUP_ID18+Type: Uint8Array
Length: 7-127 bytes
NoGroup to which the asset to be updated belongs. By default, this parameter is not specified.
  • Attributes of AssetMap in attributesToUpdate:
Attribute Name (Tag)Attribute Content (Value)MandatoryDescription
SECRETType: Uint8Array
Length: 1-1024 bytes
NoNew asset in plaintext.
DATA_LABEL_NORMAL_1Type: Uint8Array
Length: 1-2048 bytes
NoNew asset attribute information customized by the service without integrity protection.
NOTE: The data length is 1 to 512 bytes before API version 12.
DATA_LABEL_NORMAL_2Type: Uint8Array
Length: 1-2048 bytes
NoNew asset attribute information customized by the service without integrity protection.
NOTE: The data length is 1 to 512 bytes before API version 12.
DATA_LABEL_NORMAL_3Type: Uint8Array
Length: 1-2048 bytes
NoNew asset attribute information customized by the service without integrity protection.
NOTE: The data length is 1 to 512 bytes before API version 12.
DATA_LABEL_NORMAL_4Type: Uint8Array
Length: 1-2048 bytes
NoNew asset attribute information customized by the service without integrity protection.
NOTE: The data length is 1 to 512 bytes before API version 12.
DATA_LABEL_NORMAL_LOCAL_112+Type: Uint8Array
Length: 1-2048 bytes
NoLocal attribute information about the new asset. The value is assigned by the service without integrity protection and will not be synced.
DATA_LABEL_NORMAL_LOCAL_212+Type: Uint8Array
Length: 1-2048 bytes
NoLocal attribute information about the new asset. The value is assigned by the service without integrity protection and will not be synced.
DATA_LABEL_NORMAL_LOCAL_312+Type: Uint8Array
Length: 1-2048 bytes
NoLocal attribute information about the new asset. The value is assigned by the service without integrity protection and will not be synced.
DATA_LABEL_NORMAL_LOCAL_412+Type: Uint8Array
Length: 1-2048 bytes
NoLocal information about the new asset. The value is assigned by the service without integrity protection and will not be synced.

Example

NOTE

The asset module provides an asynchronous API and a synchronous API for updating an asset. The following uses the asynchronous API as an example. For more information about the APIs, see Asset Store Service.

For details about how to update an asset in a group, see Updating an asset in a Group.

Update asset demo_alias as follows: change the asset plaintext to demo_pwd_new and additional attribute to demo_label_new.

import { asset } from '@kit.AssetStoreKit';
import { util } from '@kit.ArkTS';
import { BusinessError } from '@kit.BasicServicesKit';

function stringToArray(str: string): Uint8Array {
  let textEncoder = new util.TextEncoder();
  return textEncoder.encodeInto(str);
}

let query: asset.AssetMap = new Map();
query.set(asset.Tag.ALIAS, stringToArray('demo_alias'));
let attrsToUpdate: asset.AssetMap = new Map();
attrsToUpdate.set(asset.Tag.SECRET, stringToArray('demo_pwd_new'));
attrsToUpdate.set(asset.Tag.DATA_LABEL_NORMAL_1, stringToArray('demo_label_new'));
try {
  asset.update(query, attrsToUpdate).then(() => {
    console.info(`Asset updated successfully.`);
  }).catch((err: BusinessError) => {
    console.error(`Failed to update Asset. Code is ${err.code}, message is ${err.message}`);
  });
} catch (error) {
  let err = error as BusinessError;
  console.error(`Failed to update Asset. Code is ${err.code}, message is ${err.message}`);
}

你可能感兴趣的鸿蒙文章

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 鸿蒙Adding an Asset (C/C++)

harmony 鸿蒙Managing Assets in a Group

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

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