大家好!我是黑臂麒麟(起名原因:一个出生右臂自带纹身的高质量程序员😏),也是一位6+(约2个半坤年)的前端;
学习如像练武一样,理论和实践要相结合;
如需深究可前往高级ArkTS系列课程;
望对学习鸿蒙小伙伴有所帮助;
开发鸿蒙项目中,遇到获取resources/rawfile目录下文件内容问题。记录下来,希望对学习鸿蒙的小伙伴有所帮助。
获取方式一
getRawFileContent(9+)
getRawFileContent(path: string, callback: AsyncCallback): void
用户获取resources/rawfile目录下对应的rawfile文件内容,使用callback异步回调。
参数:
参数名 | 类型 | 必填 | 说明 |
---|---|---|---|
path | string | 是 | rawfile文件路径 |
callback | AsyncCallback | 是 | 返回获取的rawfile文件内容 |
错误码:
错误码ID | 错误信息 |
---|---|
401 | If the input parameter invalid. Possible causes: Incorrect parameter types. |
示例:
我们获取rawfile文件下的
// Data.json文件
[
{
"id": 1,
"imageSrc": "app.media.enablement_pic1",
"title": "HarmonyOS第一课",
"brief": "基于真实的开发场景,提供向导式学习,多维度融合课程等内容,给开发者提供全新的学习体验。",
"webUrl": "https://developer.huawei.com/consumer/cn/doc/harmonyos-video-courses/video-tutorials-0000001443535745"
},
]
import { util } from '@kit.ArkTS'
import { BusinessError } from '@kit.BasicServicesKit';
export class ArticleClass {
id: string = '';
imageSrc: string = '';
title: string = '';
brief: string = '';
webUrl: string = '';
constructor(id: string, imageSrc: string, title: string, brief: string, webUrl: string) {
this.id = id;
this.imageSrc = imageSrc;
this.title = title;
this.brief = brief;
this.webUrl = webUrl;
}
}
@Component
export struct GetRawfile {
@State enablementList: Array<ArticleClass> = []
aboutToAppear(): void {
try {
// 这里利用getContext(this).resourceManager的getRawFileContent获取文件内容
getContext(this).resourceManager.getRawFileContent('test.txt', (_err, value) => {
let textDecoder = util.TextDecoder.create('utf-8', {
ignoreBOM: true
})
let resultPut = textDecoder.decodeToString(value);
this.enablementList = JSON.parse(resultPut) as ArticleClass[];
});
} catch (error) {
// 错误处理
let code = (error as BusinessError).code;
let message = (error as BusinessError).message;
console.error(`callback getRawFileContent failed, error code: ${code}, message: ${message}.`);
}
}
build() {
RelativeContainer() {
Text(this.message)
.id('RawfileHelloWorld')
.fontSize(50)
.fontWeight(FontWeight.Bold)
.alignRules({
center: { anchor: '__container__', align: VerticalAlign.Center },
middle: { anchor: '__container__', align: HorizontalAlign.Center }
})
}
.height('100%')
.width('100%')
}
}
获取方式二
getRawFileContent(9+)
getRawFileContent(path: string): Promise
用户获取resources/rawfile目录下对应的rawfile文件内容,使用Promise异步回调。
参数:
参数名 | 类型 | 必填 | 说明 |
---|---|---|---|
path | string | 是 | rawfile文件路径。 |
返回值:
参数名 | 说明 |
---|---|
Promise | rawfile文件内容。 |
错误码:
参数名 | 错误信息 |
---|---|
401 | If the input parameter invalid. Possible causes: Incorrect parameter types. |
9001005 | Invalid relative path. |
示例:
import { BusinessError } from '@kit.BasicServicesKit';
try {
this.context.resourceManager.getRawFileContent("test.txt").then((value: Uint8Array) => {
let rawFile = value;
}).catch((error: BusinessError) => {
console.error("getRawFileContent promise error is " + error);
});
} catch (error) {
let code = (error as BusinessError).code;
let message = (error as BusinessError).message;
console.error(`promise getRawFileContent failed, error code: ${code}, message: ${message}.`);
}
总结
以上两种方式任选其一, 都可以获取resource/rawfile文件夹下的文件内容.个人前端,习惯方式二。希望文章对小伙伴们有帮助。
结语
本篇文章的内容结束了。文章有不对或不完整的地方,望多指点;
望更多小伙伴们加入harmonyOS开发大家庭,壮大生态圈,让鸿蒙更好,让国产手机(物联网)系统更强大。
鸿蒙5.0已经在公测了。和小伙伴一起加油,鸿蒙不仅仅应用开发系统。也会在物联网等领域大展拳脚。鸿蒙之父说:“手机端只是鸿蒙的5%的潜能。应用开发只是第一步,还有更多潜能供我们发觉开发"。
如对你学习有所帮助,希望可爱你动动小手,关注、点赞、收藏;