Asset Store存储失败,怎么解决?

使用import { asset } from ‘@kit.AssetStoreKit’; set数据时报错。

05-28 13:32:53.384   42371-42371  A03D00/JSAPP                   com.xxx.example.xxxx  I     设置AS 
05-28 13:32:53.384   42371-42371  A03D00/JSAPP                   com.xxx.example.xxxx  I     AssetStore.set bbb for key aaa, storage = true 
05-28 13:32:53.384   42371-42371  C02C0B/BEGET                   com.xxx.example.xxxx  E     [init_syscap.c:51]Failed get paramName. 
05-28 13:32:53.384   42371-42371  A01111/AssetStore              com.xxx.example.xxxx  D     aaa未查询到结果,新增数据 
05-28 13:32:53.384   42371-42371  C02C0B/BEGET                   com.xxx.example.xxxx  E     [init_syscap.c:51]Failed get paramName. 
05-28 13:32:53.384   42371-42371  A01111/AssetStore              com.xxx.example.xxxx  D     AssetStore: SystemCapability.Security.Asset undfined

syscap.json 文件已添加,是还需要额外配置什么?

HarmonyOS
2024-11-07 11:51:39
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
zbw_apple

​可以参照官网文档进行查看, 其中有详细的指南:​https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/js-apis-asset-V5

参考demo:​

import { asset } from '@kit.AssetStoreKit'; 
import util from '@ohos.util'; 
 
function stringToArray(str: string): Uint8Array { 
  let textEncoder = new util.TextEncoder(); 
  return textEncoder.encodeInto(str); 
} 
 
function setAttr() { 
  let attr: asset.AssetMap = new Map(); 
  attr.set(asset.Tag.SECRET, stringToArray('demo_pwd')); 
  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')); 
  //attr.set(asset.Tag.DATA_LABEL_CRITICAL_1,stringToArray('value1')) 
  try { 
    asset.add(attr).then(() => { 
      console.info(`Asset added successfully.`); 
    }).catch(() => { 
      console.error(`Failed to add Asset.`); 
    }) 
  } catch (error) { 
    console.error(`Failed to add Asset.`); 
  } 
} 
 
function arrayToString(arr: Uint8Array): string { 
  let textDecoder = util.TextDecoder.create("utf-8", { ignoreBOM: true }); 
  let str = textDecoder.decodeWithStream(arr, { stream: false }) 
  return str; 
} 
 
function getAttr() { 
  let query: asset.AssetMap = new Map(); 
  query.set(asset.Tag.ALIAS, stringToArray('demo_alias')); // 指定了关键资产别名,最多查询到一条满足条件的关键资产 
  query.set(asset.Tag.RETURN_TYPE, asset.ReturnType.ALL);  // 此处表示需要返回关键资产的所有信息,即属性+明文 
  //query.set(asset.Tag.RETURN_TYPE, asset.ReturnType.ATTRIBUTES);  // 此处表示需要返回关键资产的所有信息,即属性 
  try { 
    asset.query(query).then((res: Array<asset.AssetMap>) => { 
      console.info('获取的资产长度为:'+res.length) 
      for (let i = 0; i < res.length; i++) { 
        // 解析密钥 
        let secret: Uint8Array = res[i].get(asset.Tag.SECRET) as Uint8Array; 
        // 将 uint8array 解析为字符串 
        let secretStr: string = arrayToString(secret); 
        console.info('资产信息为:'+secretStr) 
      } 
    }).catch (() => { 
      console.error(`Failed to query Asset.`); 
    }); 
  } catch (error) { 
    console.error(`Failed to query Asset.`); 
  } 
} 
 
@Entry 
@Component 
struct AttrTest { 
  build() { 
    Column(){ 
      Button('设置资产').onClick((event: ClickEvent) => { 
        setAttr() 
      }) 
        .height(100) 
        .width('100%') 
      Button('获取资产').onClick((event: ClickEvent) => { 
        getAttr() 
      }) 
        .height(100) 
        .width('100%') 
    } 
  } 
}
分享
微博
QQ
微信
回复
2024-11-07 17:41:17
相关问题
使用asset-store-kit 管理数据问题
187浏览 • 1回复 待解决
手机root后是否能访问Asset Store Kit
559浏览 • 1回复 待解决
有谁知道asset api调用失败怎么办啊?
1698浏览 • 1回复 待解决
Jtag不识别/OpenOCD连接失败怎么解决
11431浏览 • 2回复 待解决
调用SL_IID_BUFFERQUEUE提示失败怎么解决
542浏览 • 1回复 待解决
import asset from '@ohos.security.asset'报错
2117浏览 • 1回复 待解决
HarmonyOS如何解决数据存储问题?
9064浏览 • 3回复 已解决
图片存储解决方案谁知道啊?
2559浏览 • 1回复 待解决