场景介绍

开发者可以通过本指导了解在HarmonyOS应用中,如何使用Native Rawfile接口操作Rawfile目录和文件。功能包括遍历、打开、搜索、读取和关闭Rawfile。

接口说明

接口名

描述

NativeResourceManager *OH_ResourceManager_InitNativeResourceManager(napi_env env, napi_value jsResMgr)

初始化native resource manager。

RawDir *OH_ResourceManager_OpenRawDir(const NativeResourceManager *mgr, const char *dirName)

打开指定rawfile目录。

int OH_ResourceManager_GetRawFileCount(RawDir *rawDir)

获取指定rawfile目录下的rawfile文件数量。

const char *OH_ResourceManager_GetRawFileName(RawDir *rawDir, int index)

获取rawfile名字。

RawFile *OH_ResourceManager_OpenRawFile(const NativeResourceManager *mgr, const char *fileName)

打开指定rawfile文件。

long OH_ResourceManager_GetRawFileSize(RawFile *rawFile)

获取rawfile文件大小。

int OH_ResourceManager_SeekRawFile(const RawFile *rawFile, long offset, int whence)

指定rawfile内偏移量。

long OH_ResourceManager_GetRawFileOffset(const RawFile *rawFile)

获取rawfile偏移量。

int OH_ResourceManager_ReadRawFile(const RawFile *rawFile, void *buf, size_t length)

读取rawfile文件内容。

void OH_ResourceManager_CloseRawFile(RawFile *rawFile)

释放rawfile文件相关资源。

void OH_ResourceManager_CloseRawDir(RawDir *rawDir)

释放rawfile目录相关资源。

bool OH_ResourceManager_GetRawFileDescriptor(const RawFile *rawFile, RawFileDescriptor &descriptor)

获取rawfile的fd。

bool OH_ResourceManager_ReleaseRawFileDescriptor(const RawFileDescriptor &descriptor)

释放rawfile的fd。

void OH_ResourceManager_ReleaseNativeResourceManager(NativeResourceManager *resMgr)

释放native resource manager相关资源。

开发步骤

以Js侧获取rawfile文件列表、rawfile文件内容、rawfile描述符{fd, offset, length}三种调用方式为例。

1. 创建工程

2. 添加依赖

创建完成后,IDE会在工程生成cpp目录,目录有libentry/index.d.ts、hello.cpp、CMakeLists.txt等文件。

1.  打开src/main/cpp/CMakeLists.txt,在target_link_libraries依赖中添加资源的librawfile.z.so以及日志依赖libhilog_ndk.z.so。

target_link_libraries(entry PUBLIC libace_napi.z.so libhilog_ndk.z.so librawfile.z.so)

2.  打开src/main/cpp/types/libentry/index.d.ts文件,此文件声明了应用侧函数getFileList、getRawFileContent、getRawFileDescriptor。

import resourceManager from '@ohos.resourceManager';
export const getFileList: (resmgr: resourceManager.ResourceManager, path: string) => Array<String>;
export const getRawFileContent: (resmgr: resourceManager.ResourceManager, path: string) => Uint8Array;
export const getRawFileDescriptor: (resmgr: resourceManager.ResourceManager, path: string) => resourceManager.RawFileDescriptor;

  

3. 修改源文件

1.  打开src/main/cpp/hello.cpp文件,文件Init会对当前方法进行初始化映射,这里定义对外接口为getFileList、getRawFileContent、getRawFileDescriptor,映射C++接口分别为GetFileList、GetRawFileContent、GetRawFileDescriptor。

EXTERN_C_START
static napi_value Init(napi_env env, napi_value exports)
{
napi_property_descriptor desc[] = {
{ "getFileList", nullptr, GetFileList, nullptr, nullptr, nullptr, napi_default, nullptr },
{ "getRawFileContent", nullptr, GetRawFileContent, nullptr, nullptr, nullptr, napi_default, nullptr },
{ "getRawFileDescriptor", nullptr, GetRawFileDescriptor, nullptr, nullptr, nullptr, napi_default, nullptr }
};
napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
return exports;
}
EXTERN_C_END

  

2.  把src/main/cpp/hello.cpp文件中,增加对应的三个方法,如下所示

static napi_value GetFileList(napi_env env, napi_callback_info info)
static napi_value GetRawFileContent(napi_env env, napi_callback_info info)
static napi_value GetRawFileDescriptor(napi_env env, napi_callback_info info)

  

3.  hello.cpp文件中获取Js的资源对象,并转为Native的资源对象,即可调用资源的Native接口,获取rawfile列表、rawfile文件内容以及rawfile描述符{fd, offset, length}三种调用方式示例代码如下:

#include <rawfile/raw_file.h>
#include <rawfile/raw_dir.h>
#include <rawfile/raw_file_manager.h> // 示例一:获取rawfile文件列表 GetFileList
static napi_value GetFileList(napi_env env, napi_callback_info info)
{
OH_LOG_Print(LOG_APP, LOG_ERROR, GLOBAL_RESMGR, tag, "NDKTest Begin");
size_t requireArgc = 3;
size_t argc = 2;
napi_value argv[2] = { nullptr };
// 获取参数信息
napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
// argv[0]即为函数第一个参数Js资源对象,OH_ResourceManager_InitNativeResourceManager转为Native对象。
NativeResourceManager *mNativeResMgr = OH_ResourceManager_InitNativeResourceManager(env, argv[0]); // 获取函数argv[1],此为为rawfile相对路径
size_t strSize;
char strBuf[256];
napi_get_value_string_utf8(env, argv[1], strBuf, sizeof(strBuf), &strSize);
std::string dirName(strBuf, strSize); // 获取对应的rawDir指针对象
RawDir* rawDir = OH_ResourceManager_OpenRawDir(mNativeResMgr, dirName.c_str()); // 获取rawDir下文件及文件夹数量
int count = OH_ResourceManager_GetRawFileCount(rawDir); // 遍历获取文件名称,并保存
std::vector<std::string> tempArray;
for(int i = 0; i < count; i++) {
std::string filename = OH_ResourceManager_GetRawFileName(rawDir, i);
tempArray.emplace_back(filename);
} napi_value fileList;
napi_create_array(env, &fileList);
for (size_t i = 0; i < tempArray.size(); i++) {
napi_value jsString;
napi_create_string_utf8(env, tempArray[i].c_str(), NAPI_AUTO_LENGTH, &jsString);
napi_set_element(env, fileList, i, jsString);
} // 关闭打开的指针对象
OH_ResourceManager_CloseRawDir(rawDir);
OH_ResourceManager_ReleaseNativeResourceManager(mNativeResMgr);
return fileList;
} // 示例二:获取rawfile文件内容 GetRawFileContent
napi_value CreateJsArrayValue(napi_env env, std::unique_ptr<uint8_t[]> &data, long length)
{
napi_value buffer;
napi_status status = napi_create_external_arraybuffer(env, data.get(), length,
[](napi_env env, void *data, void *hint) {
delete[] static_cast<char*>(data);
}, nullptr, &buffer);
if (status != napi_ok) {
OH_LOG_Print(LOG_APP, LOG_ERROR, GLOBAL_RESMGR, tag, "Failed to create external array buffer");
return nullptr;
}
napi_value result = nullptr;
status = napi_create_typedarray(env, napi_uint8_array, length, buffer, 0, &result);
if (status != napi_ok) {
OH_LOG_Print(LOG_APP, LOG_ERROR, GLOBAL_RESMGR, tag, "Failed to create media typed array");
return nullptr;
}
data.release();
return result;
} static napi_value GetRawFileContent(napi_env env, napi_callback_info info)
{
OH_LOG_Print(LOG_APP, LOG_ERROR, GLOBAL_RESMGR, tag, "GetFileContent Begin");
size_t requireArgc =3;
size_t argc =2;
napi_value argv[2] = { nullptr };
// 获取参数信息
napi_get_cb_info(env, info,&argc, argv, nullptr, nullptr); // argv[0]即为函数第一个参数Js资源对象,OH_ResourceManager_InitNativeResourceManager转为Native对象。
NativeResourceManager *mNativeResMgr = OH_ResourceManager_InitNativeResourceManager(env, argv[0]);
size_t strSize;
char strBuf[256];
napi_get_value_string_utf8(env, argv[1], strBuf, sizeof(strBuf),&strSize);
std::string filename(strBuf, strSize);
// 获取rawfile指针对象
RawFile *rawFile = OH_ResourceManager_OpenRawFile(mNativeResMgr, filename.c_str());
if(rawFile != nullptr) {
OH_LOG_Print(LOG_APP, LOG_ERROR, GLOBAL_RESMGR, tag,"OH_ResourceManager_OpenRawFile success");
}
// 获取rawfile大小并申请内存
long len = OH_ResourceManager_GetRawFileSize(rawFile);
std::unique_ptr<uint8_t[]>data= std::make_unique<uint8_t[]>(len);
// 读取rawfile
int res = OH_ResourceManager_ReadRawFile(rawFile,data.get(), len);
// 关闭打开的指针对象
OH_ResourceManager_CloseRawFile(rawFile);
OH_ResourceManager_ReleaseNativeResourceManager(mNativeResMgr);
// 转为js对象
return CreateJsArrayValue(env,data, len);
} // 示例三:获取rawfile文件描述符 GetRawFileDescriptor
napi_value createJsFileDescriptor(napi_env env, RawFileDescriptor &descriptor)
{
napi_value result;
napi_status status = napi_create_object(env,&result);
if(status != napi_ok) {
returnresult;
}
napi_value fd;
status = napi_create_int32(env, descriptor.fd,&fd);
if(status != napi_ok) {
returnresult;
}
status = napi_set_named_property(env,result,"fd", fd);
if(status != napi_ok) {
returnresult;
}
napi_value offset;
status = napi_create_int64(env, descriptor.start,&offset);
if(status != napi_ok) {
returnresult;
}
status = napi_set_named_property(env,result,"offset", offset);
if(status != napi_ok) {
returnresult;
} napi_value length;
status = napi_create_int64(env, descriptor.length,&length);
if(status != napi_ok) {
returnresult;
}
status = napi_set_named_property(env,result,"length", length);
if(status != napi_ok) {
returnresult;
}
returnresult;
} static napi_value GetRawFileDescriptor(napi_env env, napi_callback_info info)
{
OH_LOG_Print(LOG_APP, LOG_ERROR, GLOBAL_RESMGR, tag,"NDKTest GetRawFileDescriptor Begin");
size_t requireArgc =3;
size_t argc =2;
napi_value argv[2] = { nullptr };
// 获取参数信息
napi_get_cb_info(env, info,&argc, argv, nullptr, nullptr); napi_valuetype valueType;
napi_typeof(env, argv[0],&valueType);
// 获取native的resourceManager对象
NativeResourceManager *mNativeResMgr = OH_ResourceManager_InitNativeResourceManager(env, argv[0]);
size_t strSize;
char strBuf[256];
napi_get_value_string_utf8(env, argv[1], strBuf, sizeof(strBuf),&strSize);
std::string filename(strBuf, strSize);
// 获取rawfile指针对象
RawFile *rawFile = OH_ResourceManager_OpenRawFile(mNativeResMgr, filename.c_str());
if(rawFile != nullptr) {
OH_LOG_Print(LOG_APP, LOG_ERROR, GLOBAL_RESMGR, tag,"OH_ResourceManager_OpenRawFile success");
}
// 获取rawfile的描述符RawFileDescriptor {fd, offset, length}
RawFileDescriptor descriptor;
OH_ResourceManager_GetRawFileDescriptor(rawFile, descriptor);
// 关闭打开的指针对象
OH_ResourceManager_CloseRawFile(rawFile);
OH_ResourceManager_ReleaseNativeResourceManager(mNativeResMgr);
// 转为js对象
return createJsFileDescriptor(env,descriptor);
}

  

4.  Js侧调用

a.  打开src\main\ets\pages\index.ets, 导入"libentry.so";

b.  获取当前js的resourceManager对象;

c.  调用Native接口getFileList即为src/main/cpp/types/libentry/index.d.ts中声明的接口,传入js的资源对象,以及rawfile文件夹的相对路径。示例如下:

import hilog from '@ohos.hilog';
import testNapi from 'libentry.so' // 导入so
@Entry
@Component
struct Index {
@State message: string = 'Hello World'
private resmgr = getContext().resourceManager; // 获取js的资源对象
build() {
Row() {
Column() {
Text(this.message)
.fontSize(50)
.fontWeight(FontWeight.Bold)
.onClick(() => {
hilog.isLoggable(0x0000, 'testTag', hilog.LogLevel.INFO);
let rawfilelist = testNapi.getFileList(this.resmgr, ""); //传入资源对象,以及访问的rawfile文件夹名称
console.log("rawfilelist" + rawfilelist);
let rawfileContet = testNapi.getRawFileContent(this.resmgr, "rawfile1.txt");
console.log("rawfileContet" + rawfileContet);
let rawfileDescriptor = testNapi.getRawFileDescriptor(this.resmgr, "rawfile1.txt");
console.log("getRawFileDescriptor" + rawfileDescriptor.fd, rawfileDescriptor.offset, rawfileDescriptor.length);
})
}
.width('100%')
}
.height('100%')
}
}

  

函数介绍

1.  根据NativeResourceManager实例,使用OH_ResourceManager_OpenRawDir接口获取RawDir实例。

RawDir* rawDir = OH_ResourceManager_OpenRawDir(nativeResourceManager, path.c_str());

  

2.  根据RawDir实例,使用OH_ResourceManager_GetRawFileCount接口获取对应目录下的rawfile文件总数 。

int count = OH_ResourceManager_GetRawFileCount(rawDir);

  

3.  根据RawDir实例,使用OH_ResourceManager_GetRawFileName接口获取目录下对应index的rawfile文件名

for (int index = 0; index < count; index++) {
std::string fileName = OH_ResourceManager_GetRawFileName(rawDir, index);
}

  

4.  根据NativeResourceManager实例,使用OH_ResourceManager_OpenRawFile接口获取指定文件名的RawFile实例。

RawFile* rawFile = OH_ResourceManager_OpenRawFile(nativeResourceManager, fileName.c_str());

  

5.  根据RawFile实例,使用OH_ResourceManager_GetRawFileSize接口获取对应rawfile文件大小。

long rawFileSize = OH_ResourceManager_GetRawFileSize(rawFile);

  

6.  根据RawFile实例,使用OH_ResourceManager_SeekRawFile接口指定rawfile偏移量。

int position = OH_ResourceManager_SeekRawFile(rawFile, 10, 0);
int position = OH_ResourceManager_SeekRawFile(rawFile, 0 , 1);
int position = OH_ResourceManager_SeekRawFile(rawFile, -10, 2);

  

7.  根据RawFile实例,使用OH_ResourceManager_GetRawFileOffset接口获取rawfile偏移量。

long rawFileOffset = OH_ResourceManager_GetRawFileOffset(rawFile);

  

8.  根据RawFile实例,使用OH_ResourceManager_ReadRawFile接口读取rawfile文件内容。

std::unique_ptr<char[]> mediaData = std::make_unique<char[]>(rawFileSize);
long rawFileOffset = OH_ResourceManager_ReadRawFile(rawFile, mediaData.get(), rawFileSize);

  

9.  根据RawFile实例,使用OH_ResourceManager_CloseRawFile接口释放rawfile文件相关资源。

OH_ResourceManager_CloseRawFile(rawFile);

  

10.  根据RawDir实例,使用OH_ResourceManager_CloseRawDir接口释放rawfile目录相关资源。

OH_ResourceManager_CloseRawDir(rawDir);

11.  根据RawFile实例,使用OH_ResourceManager_GetRawFileDescriptor接口获取rawfile的RawFileDescriptor。

RawFileDescriptor descriptor;
bool result = OH_ResourceManager_GetRawFileDescriptor(rawFile, descriptor);

  

12.  根据RawFileDescriptor实例,使用OH_ResourceManager_ReleaseRawFileDescriptor接口关闭rawfile的fd。

OH_ResourceManager_ReleaseRawFileDescriptor(descriptor);

13.  根据NativeResourceManager实例,使用OH_ResourceManager_ReleaseNativeResourceManager接口释放native resource manager。

OH_ResourceManager_ReleaseNativeResourceManager(nativeResourceManager);

  

Native Rawfile开发指导的更多相关文章

  1. 【初探IONIC】不会Native可不可以开发APP?

    前言 Hybrid技术流行已经有一段日子了,楼主的关注点也一直围绕着移动端围绕着Hybrid相关展开,Hybrid已经是大大提升开发效率的开发方式了,但是仍然需要至少一个IOS与Andriod,那么可 ...

  2. Android原生(Native)C开发之一:环境搭建篇

    引用:http://blog.sina.com.cn/s/blog_4a0a39c30100auh9.html Android是基于Linux的操作系统,处理器是ARM的,所以要在Linux或Wind ...

  3. react-native —— 在Windows下搭建React Native Android开发环境

    在Windows下搭建React Native Android开发环境 前段时间在开发者头条收藏了 @天地之灵_邓鋆 分享的<在Windows下搭建React Native Android开发环 ...

  4. Native Application 开发详解(直接在程序中调用 ntdll.dll 中的 Native API,有内存小、速度快、安全、API丰富等8大优点)

    文章目录:                   1. 引子: 2. Native Application Demo 展示: 3. Native Application 简介: 4. Native Ap ...

  5. iOS之H5和Native混合开发

    今天需要用到一个H5和Native 混合开发的项目,简单的写一点入门的东西,很简答: 先介绍一下简单的配置步骤: 1.新建项目:SB拖一个UIWebView 按住Ctrl 拖线delegate 设置为 ...

  6. react-native —— 在Mac上配置React Native Android开发环境排坑总结

    配置React Native Android开发环境总结 1.卸载Android Studio,在终端(terminal)执行以下命令: rm -Rf /Applications/Android\ S ...

  7. Native App开发 与Web App开发(原生与web开发优缺点)

    Native App开发 Native App开发即我们所称的传统APP开发模式(原生APP开发模式),该开发针对IOS.Android等不同的手机操作系统要采用不同的语言和框架进行开发,该模式通常是 ...

  8. taro之React Native 端开发研究

    初步结论:如果想把 React Native 集成到现有的原生项目中,不能使用taro的React Native 端开发功能(目前来说不能实现,以后再观察).   RN开发有2种模式: 1.一是原生A ...

  9. Android原生(Native)C开发之四:SDL移植笔记

    http://www.apkbus.com/forum.php?mod=viewthread&tid=1989 SDL(Simple DirectMedia Layer)是一套开放源码的跨平台 ...

  10. Google Chrome Native Messaging开发实录(二)Chrome Extension扩展

    接上一篇<Google Chrome Native Messaging开发实录(一)背景介绍>的项目背景,话不多说,有关Chrome Extension介绍和文档就不展开了,直接上代码. ...

随机推荐

  1. 【Azure 应用服务】App Service For Linux 中安装paping, 用于验证从App Service向外请求的网络连通性

    问题描述 App Service For Linux 中安装paping的操作步骤 解决步骤 1) 登录App Service的Kudu站点,点击Bash 2)使用命令下载paping压缩文件:#wg ...

  2. 【Azure Developer】使用Azure Key Vault 的Key签名后,离线验证的一些参考资料

    问题描述 使用 key Vault 的sign接口,Request Body中的 Value 是要传什么呢? 签名后的内容如何在本地离线验证呢? Azure Key Vault Sign 接口:htt ...

  3. 【Azure 应用服务】App Server 部署后,Docker报错,找不到8080端口

    问题描述 App Service for Container.  Docker Image 推送到ACR(向 Azure 容器注册表), 配置App Service并部署成功了.查看Docker日志( ...

  4. 一文带你了解 「图数据库」Nebula 的存储设计和思考

    本文首发于 Nebula Graph Community 公众号 在上次的 nebula-storage on nLive 直播中,来自 Nebula 存储团队的负责人王玉珏(四王)同大家分享了 ne ...

  5. Nebula Graph 源码解读系列 | Vol.05 Scheduler 和 Executor 两兄弟

    本文首发于 Nebula Graph Community 公众号 上篇我们讲述了 Query Engine Optimizer 部分的内容,在本文我们讲解下 Query Engine 剩下的 Sche ...

  6. Java 韩顺平老师的课,记的(前6章)笔记

    https://www.bilibili.com/video/BV1fh411y7R8/?p=110&spm_id_from=333.880.my_history.page.click& ...

  7. UI自动化如何保持登录状态?

    给页面设置cookie信息,刷新页面清楚缓存实现保持登录状态: 通过cookie保持登录,1手动登录,通过applacation提取登录的cookie,2通过driver.add_cookie({&q ...

  8. C++ //类模板与友元 //全局函数类内实现 -直接在类内声名由于即可 //全局函数类外实现 -需要提前让编译器知道全局函数的存在

    1 //类模板与友元 2 //全局函数类内实现 -直接在类内声名由于即可 3 //全局函数类外实现 -需要提前让编译器知道全局函数的存在 4 5 #include <iostream> 6 ...

  9. Educational Codeforces Round 145 (Rated for Div. 2)C. Sum on Subarrays(构造)

    很意思的一道构造题 题意:给一个\(n.k\),让构造长度为n的数组满足,子数组为整数的个数为k个,负数的为\(k-(n+1)* n/2\),每个数的范围为\([-1000,1000]\) 这种构造题 ...

  10. 出海业务如何搭建国内也能快速访问的https网站与接口(无需备案)

    背景信息 由于最近在搭建我的出海网站 https://www.idatariver.com/zh-cn , 感兴趣的可以看看. 其中一个环节便是给后端API接口加上ssl,毕竟http看着不如http ...