• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Rawfile开发指导
2
3
4
5## 场景介绍
6
7开发者可以通过本指导了解在OpenHarmony应用中,如何使用Native Rawfile接口操作Rawfile目录和文件。功能包括遍历、打开、搜索、读取和关闭Rawfile。
8
9## 接口说明
10
11| 接口名                                                       | 描述                                     |
12| :----------------------------------------------------------- | :--------------------------------------- |
13| NativeResourceManager *OH_ResourceManager_InitNativeResourceManager(napi_env env, napi_value jsResMgr) | 初始化native resource manager。          |
14| RawDir *OH_ResourceManager_OpenRawDir(const NativeResourceManager *mgr, const char *dirName) | 打开指定rawfile目录。                    |
15| int OH_ResourceManager_GetRawFileCount(RawDir *rawDir)       | 获取指定rawfile目录下的rawfile文件数量。 |
16| const char *OH_ResourceManager_GetRawFileName(RawDir *rawDir, int index) | 获取rawfile名字。                        |
17| RawFile *OH_ResourceManager_OpenRawFile(const NativeResourceManager *mgr, const char *fileName) | 打开指定rawfile文件。                    |
18| long OH_ResourceManager_GetRawFileSize(RawFile *rawFile)     | 获取rawfile文件大小。                    |
19| int OH_ResourceManager_SeekRawFile(const RawFile *rawFile, long offset, int whence) | 指定rawfile内偏移量。                    |
20| long OH_ResourceManager_GetRawFileOffset(const RawFile *rawFile) | 获取rawfile偏移量。                      |
21| int OH_ResourceManager_ReadRawFile(const RawFile *rawFile, void *buf, size_t length) | 读取rawfile文件内容。                    |
22| void OH_ResourceManager_CloseRawFile(RawFile *rawFile)       | 释放rawfile文件相关资源。                |
23| void OH_ResourceManager_CloseRawDir(RawDir *rawDir)          | 释放rawfile目录相关资源。                |
24| bool OH_ResourceManager_GetRawFileDescriptor(const RawFile *rawFile, RawFileDescriptor &descriptor) | 获取rawfile的fd。                        |
25| bool OH_ResourceManager_ReleaseRawFileDescriptor(const RawFileDescriptor &descriptor) | 释放rawfile的fd。                        |
26| void OH_ResourceManager_ReleaseNativeResourceManager(NativeResourceManager *resMgr) | 释放native resource manager相关资源。    |
27
28## 开发步骤
29
301. 添加头文件。
31
32    ```c++
33    #include "raw_file_manager.h"
34    ```
35
36
37
382. 使用OH_ResourceManager_InitNativeResourceManager(napi_env env, napi_value jsResMgr)接口获取NativeResourceManager实例。
39
40    ```js
41    // js侧传递js resource manager。
42    import resManager from '@ohos.resourceManager'
43    import rawfileTest from 'librawFileTest.so'
44    resManager.getResourceManager().then(resmgr => {
45        rawfileTest.testRawFile("test", resmgr, (error, value) => {
46            console.log("test rawFile");
47        })
48    });
49    ```
50
51    ```c++
52    // C++侧获取解析js侧传递的参数。
53    NativeResourceManager* nativeResourceManager = nullptr;
54    std::string path;
55    if (i == 0 && valueType == napi_string) {
56        // 解析第一个参数,参数为相对rawfile目录的文件/目录路径。
57        ......
58        path = buf.data();
59    } else if (i == 1 && valueType == napi_object) {
60        // 解析第二个参数,参数为js resource manager。
61        nativeResourceManager = OH_ResourceManager_InitNativeResourceManager(env, argv[i]);
62    }
63    ```
64
65
66
673. 根据NativeResourceManager实例,使用OH_ResourceManager_OpenRawDir接口获取RawDir实例。
68
69    ```c++
70    RawDir* rawDir = OH_ResourceManager_OpenRawDir(nativeResourceManager, path.c_str());
71    ```
72
73
74
754. 根据RawDir实例,使用OH_ResourceManager_GetRawFileCount接口获取对应目录下的rawfile文件总数 。
76
77    ```c++
78    int count = OH_ResourceManager_GetRawFileCount(rawDir);
79    ```
80
81
82
835. 根据RawDir实例,使用OH_ResourceManager_GetRawFileName接口获取目录下对应index的rawfile文件名。
84
85    ```c++
86    for (int index = 0; index < count; index++) {
87        std::string fileName = OH_ResourceManager_GetRawFileName(rawDir, index);
88    }
89    ```
90
91
92
936. 根据NativeResourceManager实例,使用OH_ResourceManager_OpenRawFile接口获取指定文件名的RawFile实例
94
95    ```c++
96    RawFile* rawFile = OH_ResourceManager_OpenRawFile(nativeResourceManager, fileName.c_str());
97    ```
98
99
100
1017. 根据RawFile实例,使用OH_ResourceManager_GetRawFileSize接口获取对应rawfile文件大小。
102
103    ```c++
104    long rawFileSize = OH_ResourceManager_GetRawFileSize(rawFile);
105    ```
106
107
108
1098. 根据RawFile实例,使用OH_ResourceManager_SeekRawFile接口指定rawfile偏移量。
110
111    ```c++
112    int position = OH_ResourceManager_SeekRawFile(rawFile, 10, 0);
113    int position = OH_ResourceManager_SeekRawFile(rawFile, 0 , 1);
114    int position = OH_ResourceManager_SeekRawFile(rawFile, -10, 2);
115    ```
116
117
118
1199. 根据RawFile实例,使用OH_ResourceManager_GetRawFileOffset接口获取rawfile偏移量。
120
121    ```c++
122    long rawFileOffset = OH_ResourceManager_GetRawFileOffset(rawFile)
123    ```
124
125
126
12710. 根据RawFile实例,使用OH_ResourceManager_ReadRawFile接口读取rawfile文件内容。
128
129    ```c++
130    std::unique_ptr<char[]> mediaData = std::make_unique<char[]>(rawFileSize);
131    long rawFileOffset = OH_ResourceManager_ReadRawFile(rawFile, mediaData.get(), rawFileSize);
132    ```
133
134
135
13611. 根据RawFile实例,使用OH_ResourceManager_CloseRawFile接口释放rawfile文件相关资源。
137
138    ```c++
139    OH_ResourceManager_CloseRawFile(rawFile);
140    ```
141
142
143
14412. 根据RawDir实例,使用OH_ResourceManager_CloseRawDir接口释放rawfile目录相关资源。
145
146    ```c++
147    OH_ResourceManager_CloseRawDir(rawDir);
148    ```
149
150
151
15213. 根据RawFile实例,使用OH_ResourceManager_GetRawFileDescriptor接口获取rawfile的RawFileDescriptor。
153
154    ```c++
155    RawFileDescriptor descriptor;
156    bool result = OH_ResourceManager_GetRawFileDescriptor(rawFile, descriptor);
157    ```
158
159
160
16114. 根据RawFileDescriptor实例,使用OH_ResourceManager_ReleaseRawFileDescriptor接口关闭rawfile的fd。
162
163    ```c++
164    OH_ResourceManager_ReleaseRawFileDescriptor(descriptor);
165    ```
166
167
168
16915. 根据NativeResourceManager实例,使用OH_ResourceManager_ReleaseNativeResourceManager接口释放native resource manager。
170
171    ```c++
172    OH_ResourceManager_ReleaseNativeResourceManager(nativeResourceManager);
173    ```
174
175