HarmonyOS Asset(关键资产存储服务)查询不到存入的数据?

代码如下:

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

@Entry
@Component
struct Index {
  build() {
    Column() {
      Button('setAssetData').onClick(() => {
        let attr: asset.AssetMap = new Map();
        attr.set(asset.Tag.SECRET, stringToArray('demo_pwd11'));
        attr.set(asset.Tag.ALIAS, stringToArray('demo_alias'));
        attr.set(asset.Tag.ACCESSIBILITY, asset.Accessibility.DEVICE_FIRST_UNLOCKED);
        attr.set(asset.Tag.DATA_LABEL_NORMAL_1, stringToArray('demo_label'));
        try {
          asset.addSync(attr);
          console.error(`Suc to add Asset.`)
        } catch (error) {
          let err = error as BusinessError;
          console.error(`Failed to add Asset. Code is ${err.code}, message is ${err.message}`);
        }
      })

      Button('getAssetData').onClick(() => {
        let query: asset.AssetMap = new Map();
        query.set(asset.Tag.ALIAS, stringToArray('demo_alias'));
        try {
          let res: Array<asset.AssetMap> = asset.querySync(query);
          for (let i = 0; i < res.length; i++) {
            if (res[i] != null) {
              const bs = res[i].get(asset.Tag.SECRET) as Uint8Array;
              if (bs) {
                console.error(`Suc to query is ${arrayToString(bs)}`);
              } else {
                console.error(`fail to query bs undefind!`);
              }
            }
          }
        } catch (error) {
          let err = error as BusinessError;
          console.error(`Failed to query Asset. Code is ${err.code}, message is ${err.message}`);
        }
      })
    }
    .width('100%')
    .height('100%')
    .backgroundColor(0xDCDCDC)
  }
}

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

function arrayToString(bs: Uint8Array): string {
  let textDecoder = util.TextDecoder.create()
  return textDecoder.decodeToString(bs)
}
阅读 479
1 个回答

示例参考如下:

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

@Entry
@Component
struct Index {
  build() {
    Column() {

      Button('setAssetData').onClick(() => {
        let attr: asset.AssetMap = new Map();
        attr.set(asset.Tag.SECRET, stringToArray('demo_pwd11'));
        attr.set(asset.Tag.ALIAS, stringToArray('demo_alias1'));
        attr.set(asset.Tag.ACCESSIBILITY, asset.Accessibility.DEVICE_FIRST_UNLOCKED);
        attr.set(asset.Tag.DATA_LABEL_NORMAL_1, stringToArray('demo_label1'));
        try {
          asset.addSync(attr);
          console.error(`Suc to add Asset.`)
        } catch (error) {
          let err = error as BusinessError;
          console.error(`Failed to add Asset. Code is ${err.code}, message is ${err.message}`);
        }
      })

      Button('getAssetData').onClick(() => {
        let query: asset.AssetMap = new Map();
        query.set(asset.Tag.ALIAS, stringToArray('demo_alias1')); // 指定了关键资产别名,最多查询到一条满足条件的关键资产
        query.set(asset.Tag.RETURN_TYPE, asset.ReturnType.ALL); // 此处表示需要返回关键资产的所有信息,即属性+明文
        try {
          let res: Array<asset.AssetMap> = asset.querySync(query);
          for (let i = 0; i < res.length; i++) {
            if (res[i] != null) {
              res[0].forEach((v, k) => {
                console.info(k + "<<<<" + v)
              })
              const bs = res[i].get(asset.Tag.SECRET) as Uint8Array;
              console.info(bs + "<<<<<<<")
              if (bs) {
                console.error(`Suc to query is ${arrayToString(bs)}`);
              } else {
                console.error(`fail to query bs undefind!`);
              }
            }
            // parse the secret.
            let secret: Uint8Array = res[i].get(asset.Tag.SECRET) as Uint8Array;
            // parse uint8array to string
            let secretStr: string = arrayToString(secret);
            console.info("<<>><<<<" + secretStr)
          }
        } catch (error) {
          let err = error as BusinessError;
          console.error(`Failed to query Asset. Code is ${err.code}, message is ${err.message}`);
        }
      })
    }
    .width('100%')
    .height('100%')
    .backgroundColor(0xDCDCDC)
  }
}

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

function arrayToString(bs: Uint8Array): string {
  let textDecoder = util.TextDecoder.create()
  return textDecoder.decodeToString(bs)
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进