HarmonyOS rawfile中的文件如何获取URI?

使用 systemShare 进行文件分享时,需要使用文件的uri去创建SharedData,但是,如果待分享的文件是位于应用资源目录下的 rawfile 文件时,如何获取目标rawfile 文件对应的 uri,目前 context.resourceManager 中只提供了 getRawFd 、getRawFileList 和 getRawFileContent 共三个与 rawfile 相关的方法,但返回值都是没有包含 uri 数据的对象

阅读 554
1 个回答

可以参考fileUri.getUriFromPath:https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/js-apis-file-fileuri-V5\#fileurigeturifrompath

rawfile目录没有对外暴露的沙箱路径,需要先将文件拷贝进应用沙箱下再操作,拷贝进沙箱参考demo:

getContext(this).resourceManager.getRawFileContent('test.txt', (_err, value) => {
  let myBuffer: ArrayBufferLike = value.buffer
  let context = getContext(this);
  //沙箱路径
  let filePath = context.filesDir + "/test.txt";
  console.log("testTag-filePath:" + filePath);
  let file = fs.openSync(filePath, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE);
  let writeLen = fs.writeSync(file.fd, myBuffer);
  console.info("testTag-write data to file succeed and size is:" + writeLen);
  fs.closeSync(file);
})
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进