HarmonyOS asset运行add方法时报错

let attr:asset.AssetMap = new Map(); 
attr.set(asset.Tag.SECRET, StringUtils.string2Uint8Array('demo_pwd')); 
attr.set(asset.Tag.ALIAS, stringToArray('demo_alias')); 
attr.set(asset.Tag.ACCESSIBILITY, asset.Accessibility.DEVICE_POWERED_ON); 
attr.set(asset.Tag.IS_PERSISTENT, true); 
attr.set(asset.Tag.DATA_LABEL_NORMAL_1, stringToArray('demo_label')); 
try { 
  asset.add(attr).then(() => { 
    log.info(`Asset added successfully.`); 
  }).catch((err: BusinessError) => { 
    log.error(`Failed to add Asset. Code is ${err.code}, message is ${err.message}`); 
  }) 
} catch (error) { 
  let err = error as BusinessError; 
  log.error(`Failed to add Asset. Code is ${err.code}, message is ${err.message}`); 
};
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.

第二行开始就报错,报错为:Cannot read property Tag of undefined。

HarmonyOS
2024-11-04 10:14:00
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
put_get

去除attr.set(asset.Tag.IS_PERSISTENT, true);,运行无异常,demo:

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 string2Uint8Array(str: string): Uint8Array { 
  if (!str) { 
    console.error('stringToUint8Array', 'params error'); 
    throw new Error('stringToUint8Array, params error'); 
  } 
  let arr: number[] = []; 
  for (let i = 0; i < str.length; ++i) { 
    arr.push(str.charCodeAt(i)); 
  } 
  return new Uint8Array(arr); 
} 
 
export function addAsset() { 
  let attr: asset.AssetMap = new Map(); 
  attr.set(asset.Tag.SECRET, string2Uint8Array('demo_pwd')); 
  attr.set(asset.Tag.ALIAS, stringToArray('demo_alias')); 
  attr.set(asset.Tag.ACCESSIBILITY, asset.Accessibility.DEVICE_POWERED_ON); 
  // attr.set(asset.Tag.IS_PERSISTENT, true); 
  attr.set(asset.Tag.DATA_LABEL_NORMAL_1, stringToArray('demo_label')); 
 
  try { 
    asset.remove(attr) 
    asset.add(attr).then(() => { 
      console.info(`Asset added successfully.`); 
    }).catch((err: BusinessError) => { 
      console.error(`Failed to add Asset. Code is ${err.code}, message is ${err.message}`); 
    }) 
  } catch (error) { 
    let err = error as BusinessError; 
    console.error(`Failed to add Asset. Code is ${err.code}, message is ${err.message}`); 
  } 
} 
 
@Entry 
@Component 
struct Index { 
  aboutToAppear(): void { 
    addAsset() 
  } 
 
  build() { 
    Column() { 
      Row() { 
        Text('Hello Huawei') 
      } 
    } 
  } 
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
分享
微博
QQ
微信
回复
2024-11-04 16:00:54
相关问题
HarmonyOS 运行HmosWorld时报错
1082浏览 • 1回复 待解决
import asset from '@ohos.security.asset'报错
3206浏览 • 1回复 待解决
HarmonyOS 应用发布时报错
757浏览 • 1回复 待解决
HarmonyOS 调用相册函数时报错
826浏览 • 1回复 待解决
HarmonyOS 加载激励视频时报错
1238浏览 • 1回复 待解决
HarmonyOS 启动rn项目时报错
1040浏览 • 1回复 待解决
拉起UIAbility时报错16000050
3432浏览 • 1回复 待解决
HarmonyOS 使用flutter创建packages时报错
843浏览 • 1回复 待解决
HarmonyOS 获取推送token时报错1000900010
1193浏览 • 1回复 待解决
HarmonyOS 部署hap调试时报错9568393
707浏览 • 1回复 待解决
HarmonyOS DevEco调试Flutter项目时报错
795浏览 • 1回复 待解决
HarmonyOS IDE运行报错
809浏览 • 1回复 待解决
应用运行报错内存不足闪退解决方法
329浏览 • 0回复 待解决
HarmonyOS NDK 运行编译报错
1260浏览 • 1回复 待解决
使用ohpm发布tgz文件时报错
1726浏览 • 1回复 待解决