openharmony 鸿蒙 asset-js-query

2025-06-12 浏览 (1)

Querying Assets (ArkTS)

Available APIs

You can use query(query: AssetMap), an asynchronous API, and querySync(query: AssetMap), a synchronous API, to query assets.

The following table describes the attributes of AssetMap for querying 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.

Attribute Name (Tag)ValueMandatoryDescription
ALIASType: Uint8Array
Length: 1-256 bytes
NoAsset 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
NoType of sync supported by the asset.
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.
RETURN_TYPEType: number
Value range: see ReturnType
NoType of the asset query result to return.
RETURN_LIMITType: numberNoMaximum number of asset records to return.
RETURN_OFFSETType: number
Value range: 1-65536
NoOffset of the asset query result.
NOTE: This parameter specifies the starting asset record to return in batch asset query.
RETURN_ORDERED_BYType: number
Value: asset.Tag.DATA_LABEL_xxx.
NoHow the query results are sorted. Currently, the results can be sorted only by DATA_LABEL.
NOTE: By default, assets are returned in the order in which they are added.
REQUIRE_ATTR_ENCRYPTED14+Type: booleanNoWhether to query the customized asset attribute information that is encrypted. By default, the unencrypted, customized asset attribute information is queried.
GROUP_ID18+Type: Uint8Array
Length: 7-127 bytes
NoGroup to which the asset to be queried belongs. By default, this parameter is not specified.

Constraints

The assets queried are transmitted to the service through an IPC channel. Due to the limitation of the IPC buffer size, the maximum number of assets to be queried at a time cannot exceed 40.

Example

NOTE

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

For details about how to query the plaintext of an asset in a group, see Querying the Plaintext of an Asset in a Group. For details about how to query the attributes of an asset in a group, see Querying the Attributes of an Asset in a Group.

Querying the Plaintext of an Asset

Query the plaintext of asset demo_alias.

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);
}

function arrayToString(arr: Uint8Array): string {
  let textDecoder = util.TextDecoder.create("utf-8", { ignoreBOM: true });
  let str = textDecoder.decodeToString(arr, { stream: false })
  return str;
}

let query: asset.AssetMap = new Map();
query.set(asset.Tag.ALIAS, stringToArray('demo_alias')); // Specify the alias of the asset to query.
query.set(asset.Tag.RETURN_TYPE, asset.ReturnType.ALL); // Return all asset information, including attributes and asset plaintext.
try {
  asset.query(query).then((res: Array<asset.AssetMap>) => {
    for (let i = 0; i < res.length; i++) {
      // Parse the secret.
      let secret: Uint8Array = res[i].get(asset.Tag.SECRET) as Uint8Array;
      // Convert uint8array to string
      let secretStr: string = arrayToString(secret);
    }
  }).catch ((err: BusinessError) => {
    console.error(`Failed to query Asset. Code is ${err.code}, message is ${err.message}`);
  });
} catch (error) {
  let err = error as BusinessError;
  console.error(`Failed to query Asset. Code is ${err.code}, message is ${err.message}`);
}

Querying Attributes of an Asset

Query attributes of asset demo_alias.

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')); // Specify the alias of the asset to query.
query.set(asset.Tag.RETURN_TYPE, asset.ReturnType.ATTRIBUTES); // Return only the attributes of the asset, that is, the result does not include the asset plaintext.
try {
  asset.query(query).then((res: Array<asset.AssetMap>) => {
    for (let i = 0; i < res.length; i++) {
      // Parse the attributes.
      let accessibility: number = res[i].get(asset.Tag.ACCESSIBILITY) as number;
    }
  }).catch ((err: BusinessError) => {
    console.error(`Failed to query Asset. Code is ${err.code}, message is ${err.message}`);
  });
} catch (error) {
  let err = error as BusinessError;
  console.error(`Failed to query Asset. Code is ${err.code}, message is ${err.message}`);
}

Querying Attributes of Assets

Query attributes of assets with tag 1 of demo_label and return a total of 10 records sorted by DATA_LABEL_NORMAL_1 starting from the fifth record that matches the search criteria.

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.RETURN_TYPE, asset.ReturnType.ATTRIBUTES); // Return only the attributes of the asset, that is, the result does not include the asset plaintext.
query.set(asset.Tag.DATA_LABEL_NORMAL_1, stringToArray('demo_label'));
query.set(asset.Tag.RETURN_OFFSET, 5); // Return results from the fifth asset that matches the search criteria.
query.set(asset.Tag.RETURN_LIMIT, 10); // Return information about 10 assets that match the search criteria.
query.set(asset.Tag.RETURN_ORDERED_BY, asset.Tag.DATA_LABEL_NORMAL_1); // Sort the query results by DATA_LABEL_NORMAL_1.
try {
  asset.query(query).then((res: Array<asset.AssetMap>) => {
    for (let i = 0; i < res.length; i++) {
      // Parse the attributes.
      let accessibility: number = res[i].get(asset.Tag.ACCESSIBILITY) as number;
    }
  }).catch ((err: BusinessError) => {
    console.error(`Failed to query Asset. Code is ${err.code}, message is ${err.message}`);
  });
} catch (error) {
  let err = error as BusinessError;
  console.error(`Failed to query 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 鸿蒙Removing Assets (ArkTS)

harmony 鸿蒙Updating an Asset (ArkTS)

harmony 鸿蒙Adding an Asset (C/C++)

harmony 鸿蒙Managing Assets in a Group

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

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