har模块中的资源可以通过@ohos.resourceManager (资源管理)获取,hsp中的资源可以通过application的application.createModuleContext接口创建相应模块的context,再通过resourceManager获取。
示例如下:
import { application, common } from '@kit.AbilityKit';
import { BusinessError } from '@kit.BasicServicesKit';
import { buffer } from '@kit.ArkTS';
@Entry
@Component
struct Index {
private context = this.getUIContext().getHostContext() as common.UIAbilityContext;
build() {
Column() {
Button('get rawFile content')
.onClick(() => {
application.createModuleContext(this.context, 'hsp')
.then((data) => {
let rawFileData = data.resourceManager.getRawFileContentSync('hsp.txt');
let hspContent: string = buffer.from(rawFileData.buffer).toString();
})
.catch((error: BusinessError) => {
console.error(`createModuleContext failed, error.code: ${error.code}, error.message: ${error.message}`);
})
})
}
.height('100%')
.width('100%')
.justifyContent(FlexAlign.Center)
}
}合作咨询
我们的专家服务团队将竭诚为您提供专业的合作咨询服务
解决方案
精准高效的一站式服务支持,助力开发者商业成功