• 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++ 工程,在cpp目录下的index.d.ts中定义对外js接口,同时在c++中实现接口,映射关系如下:
48
49| index.d.ts中的接口名                  | rawfile_demo.cpp对应的实现函数                                                                             |
50|:---------------------------------|:----------------------------------------------------------------------------------------------------|
51| getFileList                      | GetFileList                                                                                         |
52| getRawFileContent                | GetRawFileContent                                                                                   |
53| getRawFileContent64              | GetRawFileContent64                                                                                 |
54| getRawFileDescriptor             | GetRawFileDescriptor                                                                                |
55| getRawFileDescriptor64           | GetRawFileDescriptor64                                                                              |
56| releaseRawFileDescriptor         | ReleaseRawFileDescriptor                                                                            |
57| releaseRawFileDescriptor64       | ReleaseRawFileDescriptor64                                                                          |
58
59通过获取Js的资源对象,并转为Native的资源对象,即可调用资源的Native接口,获取rawfile列表、rawfile文件内容以及rawfile描述符{fd, offset, length}。
60在Js侧导入"libentry.so",通过getContext().resourceManager获取资源管理对象。调用src/main/cpp/types/libentry/index.d.ts中声明的接口,传入js的资源对象和相关参数获取对于rawfile相关资源信息。
61源码参考:[rawfile_demo.cpp](entry/src/main/cpp/rawfile_demo.cpp)
62。 涉及到的native rawfile相关接口:
63
64| 接口名                            | 描述                                                                                                     |
65|:-------------------------------|:-------------------------------------------------------------------------------------------------------|
66| 初始化native resource manager。    | NativeResourceManager *OH_ResourceManager_InitNativeResourceManager(napi_env env, napi_value jsResMgr) |
67| 打开指定rawfile目录。                 | RawDir *OH_ResourceManager_OpenRawDir(const NativeResourceManager *mgr, const char *dirName)           |
68| 获取指定rawfile目录下的rawfile文件数量。    | int OH_ResourceManager_GetRawFileCount(RawDir *rawDir)                                                 |
69| 获取rawfile名字。                   | const char *OH_ResourceManager_GetRawFileName(RawDir *rawDir, int index)                               |
70| 打开指定rawfile文件。                 | RawFile *OH_ResourceManager_OpenRawFile(const NativeResourceManager *mgr, const char *fileName)        |
71| 获取rawfile文件大小。                 | long OH_ResourceManager_GetRawFileSize(RawFile *rawFile)                                               |
72| 读取rawfile文件内容。                 | int OH_ResourceManager_ReadRawFile(const RawFile *rawFile, void *buf, size_t length)                   |
73| 指定rawfile文件的偏移量。               | int OH_ResourceManager_SeekRawFile(const RawFile *rawFile, long offset, int whence)                    |
74| 获取rawfile文件的偏移量。               | long OH_ResourceManager_GetRawFileOffset(const RawFile *rawFile)                                       |
75| 释放rawfile文件相关资源。               | void OH_ResourceManager_CloseRawFile(RawFile *rawFile)                                                 |
76| 释放rawfile目录相关资源。               | void OH_ResourceManager_CloseRawDir(RawDir *rawDir)                                                    |
77| 获取rawfile的fd。                  | bool OH_ResourceManager_GetRawFileDescriptor(const RawFile *rawFile, RawFileDescriptor &descriptor)    |
78| 关闭rawfile的fd。                  | bool OH_ResourceManager_ReleaseRawFileDescriptor(const RawFileDescriptor &descriptor)                  |
79| 释放native resource manager相关资源。 | void OH_ResourceManager_ReleaseNativeResourceManager(NativeResourceManager *resMgr)                    |
80
81
82
83### 相关权限
84
85不涉及。
86
87### 依赖
88
89不涉及。
90
91### 约束与限制
92
931. 本示例仅支持标准系统上运行,支持设备:RK3568。
94
952. 本示例为Stage模型,支持API Version 15版本SDK,版本号:5.0.3.135,最低兼容API Version 11,镜像版本号:OpenHarmony-5.0.3.137 Release
96
973. 本示例需要使用DevEco Studio NEXT Developer Beta1(5.0.3.404)及以上版本才可编译运行。
98
99### 下载
100
101如需单独下载本工程,执行如下命令:
102
103```
104git init
105git config core.sparsecheckout true
106echo code/BasicFeature/Native/NdkRawfile/ > .git/info/sparse-checkout
107git remote add origin https://gitee.com/openharmony/applications_app_samples.git
108git pull origin master
109```
110
111