使用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;    
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.

映射接口

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; 
} 
​ 
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.

获取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); 
} 
​​
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
  • 97.
  • 98.
  • 99.
  • 100.
  • 101.
  • 102.
  • 103.
  • 104.
  • 105.
  • 106.
  • 107.
  • 108.
  • 109.
  • 110.
  • 111.
  • 112.
  • 113.
  • 114.
  • 115.
  • 116.
  • 117.
  • 118.
  • 119.
  • 120.
  • 121.
  • 122.
  • 123.
  • 124.
  • 125.
  • 126.
  • 127.
  • 128.
  • 129.
  • 130.
  • 131.
  • 132.
  • 133.
  • 134.
  • 135.
  • 136.
  • 137.
  • 138.
  • 139.
  • 140.
  • 141.
  • 142.
  • 143.
  • 144.
  • 145.
  • 146.
  • 147.
  • 148.
  • 149.
  • 150.
  • 151.
  • 152.
  • 153.
  • 154.
  • 155.
  • 156.
  • 157.
  • 158.
  • 159.

实现效果

注明适配的版本信息

DevEco Studio Version: 4.1.1.400

SDK:HarmoneyOS 4.1.0.25

分享
微博
QQ
微信
回复
2024-05-22 16:02:43
相关问题
使用RawFile
619浏览 • 1回复 待解决
HarmonyOS 如何遍历resources/rawfile目录
572浏览 • 1回复 待解决
Native获取Rawfile的内容并打印
1288浏览 • 1回复 待解决
native层如何访问rawfile的二进制文件
2676浏览 • 1回复 待解决
HarmonyOS 读取本地RawFile文件失败
1203浏览 • 1回复 待解决
Preview是否支持读取rawfile文件
3445浏览 • 1回复 待解决
Native侧如何获取可操作文件目录
3118浏览 • 1回复 待解决
HarmonyOS Rawfile中的json文件读取
958浏览 • 1回复 待解决
HarmonyOS rawfile文件拷贝到沙箱
1105浏览 • 1回复 待解决
提问
该提问已有0人参与 ,帮助了0人