HarmonyOS 获取RawFile资源文件夹下的文件
API版本:9
文档:3.1/4.0 资源管理
获取方式:
getRawFileContent9+
getRawFileContent(path: string, callback: AsyncCallback): void
用户获取resources/rawfile目录下对应的rawfile文件内容,使用callback形式返回字节数组。
系统能力:SystemCapability.Global.ResourceManager
参数:
参数名 | 类型 | 必填 | 说明 |
---|---|---|---|
path | string | 是 | rawfile文件路径 |
callback | AsyncCallback | 是 | 异步回调,用于返回获取的rawfile文件内容 |
以下错误码的详细介绍请参见资源管理错误码。
错误码:
错误码ID | 错误信息 |
---|---|
9001005 | If the resource not found by path. |
示例:
try {
this.context.resourceManager.getRawFileContent("test.xml", (error, value) => {
if (error != null) {
console.log("error is " + error);
} else {
let rawFile = value;
}
});
} catch (error) {
console.error(`callback getRawFileContent failed, error code: ${error.code}, message: ${error.message}.`)
}
getRawFileContent(path: string): Promise<Uint8Array>
getRawFileContent9+
getRawFileContent(path: string): Promise
用户获取resources/rawfile目录下对应的rawfile文件内容,使用Promise形式返回字节数组。
系统能力:SystemCapability.Global.ResourceManager
参数:
参数名 | 类型 | 必填 | 说明 |
---|---|---|---|
path | string | 是 | rawfile文件路径 |
返回值:
类型 | 说明 |
---|---|
Promise | rawfile文件内容 |
以下错误码的详细介绍请参见资源管理错误码。
错误码:
错误码ID | 错误信息 |
---|---|
9001005 | If the resource not found by path. |
示例:
try {
this.context.resourceManager.getRawFileContent("test.xml").then(value => {
let rawFile = value;
}).catch(error => {
console.log("getRawFileContent promise error is " + error);
});
} catch (error) {
console.error(`promise getRawFileContent failed, error code: ${error.code}, message: ${error.message}.`)
}