> 文档中心 > 鸿蒙读取资源文件

鸿蒙读取资源文件

在鸿蒙读取外部资源文件的API为getRawFile

但是这个方法的使用有些蹊跷

按照官方文档:

文档中心https://developer.harmonyos.com/cn/docs/documentation/doc-guides/basic-resource-file-example-0000001051733014#section126348226453首先把外部资源文件存放在resource/rawfile内

读取时只需要

this.getResourceManager().getRawFileEntry("resources/rawfile/models");

即可读取,这个resources/rawfile不能去掉,必须保留才能读取得到

有一次在使用

this.getResourceManager().getRawFileEntry("models");无法读取文件,原因就在这里

然而还有一种情况,就是到了后面openrawfile就会出错

this.getResourceManager().getRawFileEntry("resources/rawfile/labels/labelmap”).openRawFile()会直接报错

java.io.FileNotFoundException: resources/rawfile/labels/labelmap

此时要改成“entry/resources/rawfile/labels/labelmap” 加上entry就不会报错

所以保险一点还是要有entry