在native侧创建file并保存进沙箱路径

在native侧创建.txt文件并保存字符串

HarmonyOS
2024-05-22 23:34:34
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
进击的鱼白

使用的核心API

napi_create_arraybuffer
napi_create_typedarray
file.write()
file.close()

核心代码解释

static napi_value CreateUint8Array(napi_env env, napi_callback_info info) 
{ 
   size_t argc = 1; 
   napi_value args[1] = {nullptr}; 
​ 
   napi_get_cb_info(env, info, &argc, args , nullptr, nullptr); 
​ 
   size_t size; 
   //创建字符串 
   napi_status status = napi_get_value_string_utf8(env, args[0], nullptr, 0, &size); 
   if (size > 0) { 
       char resultChar[size + 1]; 
       status = napi_get_value_string_utf8(env, args[0], resultChar, size + 1, &size); 
       //打开文件保存路径 
       auto filePath = new std::string(resultChar); 
        
       OH_LOG_Print(LOG_APP, LOG_INFO, LOG_DOMAIN, "NapiTest", "保存路径:%{public}s", filePath->c_str()); 
        
       napi_value buffer; 
       size_t size = 3; 
       uint8_t uint8Data[] = {'a', 'b', 'c'}; 
       void *data = NULL; 
       //将字符串写进best1.txt中 
       std::ofstream file(*filePath + "/best1.txt", std::ios::out | std::ios::binary); 
       file.write(reinterpret_cast<const char *>(uint8Data), size); 
       file.close(); 
​ 
        
       status = napi_create_arraybuffer(env, size * sizeof(uint8_t), &data, &buffer); 
       napi_value jpeg; 
       status = napi_create_typedarray(env, napi_uint8_array, size, buffer, 0, &jpeg); 
​ 
​ 
       // 给output_ptr、output_buffer赋值 
       uint8_t *output_bytes = (uint8_t *)data; 
       for (int32_t i = 0; i < size; i++) { 
           output_bytes[i] = uint8Data[i]; 
      } 
​ 
       OH_LOG_Print(LOG_APP, LOG_INFO, LOG_DOMAIN, "NapiTest", "status == napi_ok:%{public}d", 
                    status == napi_ok); 
        
       napi_typedarray_type type; 
       napi_value input_buffer; 
       size_t byte_offset; 
       size_t length; 
       status = napi_get_typedarray_info(env, jpeg, &type, &length, NULL, &input_buffer, &byte_offset); 
       OH_LOG_Print(LOG_APP, LOG_INFO, LOG_DOMAIN, "NapiTest", "status == napi_ok:%{public}d", status == napi_ok); 
        
       void *arraybufferData; 
       size_t byte_length; 
       napi_get_arraybuffer_info(env, input_buffer, &arraybufferData, &byte_length); 
       if (type == napi_uint8_array) { 
           uint8_t *data_bytes = (uint8_t *)(data); 
           for (int i = 0; i < length / sizeof(uint8_t); i++) { 
               OH_LOG_Print(LOG_APP, LOG_INFO, LOG_DOMAIN, "NapiTest", "data_bytes:%{public}d", 
                            *((uint8_t *)(data_bytes) + i)); 
          } 
      } 
​ 
       const char *mData = reinterpret_cast<const char *>(arraybufferData); 
       std::ofstream file2(*filePath + "/best2.txt", std::ios::out | std::ios::binary); 
       file2.write(mData, length); 
       file2.close(); 
​ 
       OH_LOG_Print(LOG_APP, LOG_INFO, LOG_DOMAIN, "NapiTest", "CreateUint8Array:%{public}s", filePath->c_str()); 
       return jpeg; 
  } 
   OH_LOG_Print(LOG_APP, LOG_INFO, LOG_DOMAIN, "NapiTest", "CreateUint8Array:%{public}d", 0); 
   return nullptr; 
}

注明适配的版本信息

IDE版本:4.1.3.600

SDK版本:DP2

分享
微博
QQ
微信
回复
2024-05-23 21:22:37
相关问题
Native创建线程是否有限制
2207浏览 • 1回复 待解决
将rawfile中zip复制解压到沙箱路径
1486浏览 • 1回复 待解决
Native进行跨模块加载
847浏览 • 1回复 待解决
Native释放ArkTS对象的方法
513浏览 • 1回复 待解决
native创建socket异常
489浏览 • 1回复 待解决
arktshashmap转为native
979浏览 • 1回复 待解决
Native调用ArkTS的全局普通方法
929浏览 • 1回复 待解决
Native如何集成三方SO库
2257浏览 • 1回复 待解决
沙箱路径的文件怎么拿取?
1854浏览 • 1回复 待解决