【鸿蒙】Hsp模块 resources -> rawfile 内文件获取,附加Uint8Array解析

问题描述:

在hsp模块中的页面使用getContext(this).resourceManager.getRawFileContentSync('xxx')无法读取到Hsp模块resources -> rawfile里的文件文件, 但是把文件放在主模块中的rawfile中可以正常读取到
在这里插入图片描述


原因分析:

猜测模块之间有隔离机制。


解决方案:

getContext(this).resourceManager.getRawFileContentSync('xxx')修改为this.context.createModuleContext('library').resourceManager.getRawFileContentSync("xxx.js")
具体demo方法如下,附加json格式解析

  static rawFile(context:common.UIAbilityContext){
    let data:Uint8Array = context.createModuleContext("polestarsdk").resourceManager.getRawFileContentSync("privacyconfig.json")
    try {
      let json = String.fromCharCode(...data)
      console.info("fromCharCode = " + json);

      let textDecoderOptions: util.TextDecoderOptions = {
        // fatal: false,
        ignoreBOM : true
      }
      let decodeWithStreamOptions: util.DecodeWithStreamOptions = {
        stream: false
      }
      let textDecoder = util.TextDecoder.create('utf-8', textDecoderOptions);
      let retStr = textDecoder.decodeWithStream(data , decodeWithStreamOptions);
      console.info("retStr = " + retStr);

      let bufferStr = buffer.from(data.buffer);
      console.info("bufferStr = " + bufferStr);
    }catch (e){
      console.info('Cktest ' + JSON.stringify(e))
    }
  }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值