• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# 获取Rawfile资源
2
3### 介绍
4
5本示例中主要介绍开发者如何使用Native Rawfile接口操作Rawfile目录和文件。功能包括文件列表遍历、文件打开、搜索、读取和关闭Rawfile。
6
7### 效果预览
8
9| 主页                                   |
10|--------------------------------------|
11| ![main](screenshots/device/main.png) |
12
13使用说明
14
15应用界面中展示了Rawfile相关的接口调用,包括获取resources/rawfile目录下的文件及目录结构、对应的rawfile文件内容、对应rawfile文件的descriptor。其中使用到的功能包括文件列表遍历、文件打开、搜索、读取和关闭Rawfile。
16
17
18### 工程目录
19
20```
21entry/src/main/
22|---cpp
23|   |---types
24|   |   |---libentry
25|   |   |   |---index.d.ts                     // 接口导出
26|   |   |   |---oh-package.json5
27|   |---CMakeLists.txt                         // 编译脚本
28|   |---rawfile_demo.cpp                       // 调用native接口
29|---ets
30|   |---entryability
31|   |   |---EntryAbility.ets
32|   |---pages
33|   |   |---Index.ets                          // 首页
34|   |---utils
35|   |   |---Logger.ets                         // 日志工具
36|---resources
37|   |---rawfile                                // rawfile资源
38|   |   |---subrawfile
39|   |   |   |---rawfile2.txt
40|   |   |---rawfile.txt
41|   |   |---rawfile1.txt
42
43```
44
45### 具体实现
46
47通过在IDE中创建Native c++ 工程,在c++代码中定义对外接口为getFileList、getRawFileContent、getRawFileDescriptor,映射C++接口分别为GetFileList、GetRawFileContent、GetRawFileDescriptor。
48通过获取Js的资源对象,并转为Native的资源对象,即可调用资源的Native接口,获取rawfile列表、rawfile文件内容以及rawfile描述符{fd, offset, length}。
49在Js侧导入"libentry.so",通过getContext().resourceManager获取资源管理对象。调用src/main/cpp/types/libentry/index.d.ts中声明的接口,传入js的资源对象和相关参数获取对于rawfile相关资源信息。
50源码参考:[rawfile_demo.cpp](entry/src/main/cpp/rawfile_demo.cpp)
51。 涉及到的相关接口:
52
53| 接口名                            | 描述                                                                                                     |
54|:-------------------------------|:-------------------------------------------------------------------------------------------------------|
55| 初始化native resource manager。    | NativeResourceManager *OH_ResourceManager_InitNativeResourceManager(napi_env env, napi_value jsResMgr) |
56| 打开指定rawfile目录。                 | RawDir *OH_ResourceManager_OpenRawDir(const NativeResourceManager *mgr, const char *dirName)           |
57| 获取指定rawfile目录下的rawfile文件数量。    | int OH_ResourceManager_GetRawFileCount(RawDir *rawDir)                                                 |
58| 获取rawfile名字。                   | const char *OH_ResourceManager_GetRawFileName(RawDir *rawDir, int index)                               |
59| 打开指定rawfile文件。                 | RawFile *OH_ResourceManager_OpenRawFile(const NativeResourceManager *mgr, const char *fileName)        |
60| 获取rawfile文件大小。                 | long OH_ResourceManager_GetRawFileSize(RawFile *rawFile)                                               |
61| 读取rawfile文件内容。                 | int OH_ResourceManager_ReadRawFile(const RawFile *rawFile, void *buf, size_t length)                   |
62| 释放rawfile文件相关资源。               | void OH_ResourceManager_CloseRawFile(RawFile *rawFile)                                                 |
63| 释放rawfile目录相关资源。               | void OH_ResourceManager_CloseRawDir(RawDir *rawDir)                                                    |
64| 获取rawfile的fd。                  | bool OH_ResourceManager_GetRawFileDescriptor(const RawFile *rawFile, RawFileDescriptor &descriptor)    |
65| 释放native resource manager相关资源。 | void OH_ResourceManager_ReleaseNativeResourceManager(NativeResourceManager *resMgr)                    |
66
67
68
69### 相关权限
70
71不涉及。
72
73### 依赖
74
75不涉及。
76
77### 约束与限制
78
791. 本示例仅支持标准系统上运行,支持设备:RK3568。
80
812. 本示例为Stage模型,支持API10版本SDK,SDK版本号(API Version 10 Release),镜像版本号(4.0 Release)
82
833. 本示例需要使用DevEco Studio 版本号(4.0 Release)及以上版本才可编译运行。
84
85### 下载
86
87如需单独下载本工程,执行如下命令:
88
89```
90git init
91git config core.sparsecheckout true
92echo code/BasicFeature/Native/NdkRawfile/ > .git/info/sparse-checkout
93git remote add origin https://gitee.com/openharmony/applications_app_samples.git
94git pull origin master
95```
96
97