使用Native Rawfile接口操作Rawfile目录和文件

使用Native Rawfile接口操作Rawfile目录和文件

HarmonyOS
2024-05-20 22:14:07
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
put_get

在HarmonyOS应用开发中,经常会用到资源管理,如何使用Native Rawfile接口操作Rawfile目录和文件。功能包括文件列表遍历、文件打开、搜索、读取和关闭Rawfile。

使用的核心API

Rawfile

核心代码解释

添加依赖,声明了应用侧函数getFileList、getRawFileContent、getRawFileDescriptor。

target_link_libraries(entry PUBLIC libace_napi.z.so libhilog_ndk.z.so librawfile.z.so) 
​ 
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;    

映射接口

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; 
} 
​ 
}

获取Js的资源对象,并转为Native的资源对象,即可调用资源的Native接口,获取rawfile列表、rawfile文件内容以及rawfile描述符

static napi_value GetFileList(napi_env env, napi_callback_info info) 
{ 
  OH_LOG_Print(LOG_APP, LOG_INFO, GLOBAL_RESMGR, TAG, "GetFileList Begin"); 
  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); 
  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); 
  RawDir *rawDir = OH_ResourceManager_OpenRawDir(mNativeResMgr, filename.c_str()); 
  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; 
} 
​ 
namespace { 
  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_INFO, GLOBAL_RESMGR, TAG, "GetFileContent Begin"); 
  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); 
  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 = OH_ResourceManager_OpenRawFile(mNativeResMgr, filename.c_str()); 
  if (rawFile != nullptr) { 
      OH_LOG_Print(LOG_APP, LOG_INFO, GLOBAL_RESMGR, TAG, "OH_ResourceManager_OpenRawFile success"); 
  } 
  long len = OH_ResourceManager_GetRawFileSize(rawFile); 
  std::unique_ptr<uint8_t[]> data = std::make_unique<uint8_t[]>(len); 
​ 
  int res = OH_ResourceManager_ReadRawFile(rawFile, data.get(), len); 
​ 
  OH_ResourceManager_CloseRawFile(rawFile); 
  OH_ResourceManager_ReleaseNativeResourceManager(mNativeResMgr); 
  return CreateJsArrayValue(env, data, len); 
} 
​ 
namespace { 
  napi_value createJsFileDescriptor(napi_env env, RawFileDescriptor &descriptor) 
  { 
      napi_value result; 
      napi_status status = napi_create_object(env, &result); 
      if (status != napi_ok) { 
          return result; 
      } 
​ 
      napi_value fd; 
      status = napi_create_int32(env, descriptor.fd, &fd); 
      if (status != napi_ok) { 
          return result; 
      } 
      status = napi_set_named_property(env, result, "fd", fd); 
      if (status != napi_ok) { 
          return result; 
      } 
​ 
      napi_value offset; 
      status = napi_create_int64(env, descriptor.start, &offset); 
      if (status != napi_ok) { 
          return result; 
      } 
      status = napi_set_named_property(env, result, "offset", offset); 
      if (status != napi_ok) { 
          return result; 
      } 
​ 
      napi_value length; 
      status = napi_create_int64(env, descriptor.length, &length); 
      if (status != napi_ok) { 
          return result; 
      } 
      status = napi_set_named_property(env, result, "length", length); 
      if (status != napi_ok) { 
          return result; 
      } 
      return result; 
  } 
} 
​ 
static napi_value GetRawFileDescriptor(napi_env env, napi_callback_info info) 
{ 
  OH_LOG_Print(LOG_APP, LOG_INFO, GLOBAL_RESMGR, TAG, "GetRawFileDescriptor Begin"); 
  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); 
  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 = OH_ResourceManager_OpenRawFile(mNativeResMgr, filename.c_str()); 
  if (rawFile != nullptr) { 
      OH_LOG_Print(LOG_APP, LOG_INFO, GLOBAL_RESMGR, TAG, "OH_ResourceManager_OpenRawFile success"); 
  } 
  RawFileDescriptor descriptor; 
  OH_ResourceManager_GetRawFileDescriptor(rawFile, descriptor); 
​ 
  OH_ResourceManager_CloseRawFile(rawFile); 
  OH_ResourceManager_ReleaseNativeResourceManager(mNativeResMgr); 
  return createJsFileDescriptor(env, descriptor); 
} 
​​

实现效果

注明适配的版本信息

DevEco Studio Version: 4.1.1.400

SDK:HarmoneyOS 4.1.0.25

分享
微博
QQ
微信
回复
2024-05-22 16:02:43
相关问题
Native获取Rawfile的内容并打印
799浏览 • 1回复 待解决
native层如何访问rawfile的二进制文件
1955浏览 • 1回复 待解决
Preview是否支持读取rawfile文件
2488浏览 • 1回复 待解决
HarmonyOS 读取本地RawFile文件失败
39浏览 • 1回复 待解决
Native侧如何获取可操作文件目录
2090浏览 • 1回复 待解决
HarmonyOS RawFileRawFile64区别
27浏览 • 1回复 待解决
hsp模块里rawfile文件读取问题
319浏览 • 1回复 待解决
HarmonyOS rawFile文件夹合并问题
49浏览 • 1回复 待解决
HarmonyOS rawfile中的文件如何获取URI
37浏览 • 1回复 待解决
HarmonyOS 三方so如何访问rawfile文件
348浏览 • 1回复 待解决