目录

How do I obtain the rawfile resources from a HAR/HSP in a multi-module project?

For the rawfile resources in a HAR, you can obtain them through @ohos.resourceManager (Resource Management). For resources in an HSP, you can create context of the corresponding module using the application.createModuleContext API of the application and then obtain them through resourceManager.

The code snippet is as follows:

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)
  }
}