HarmonyOS 请求报错如下 \{"code":2300026,"message":"Failed to open/read local data from file/application"\}?

如题:HarmonyOS 请求报错如下 {"code":2300026,"message":"Failed to open/read local data from file/application"}?

阅读 520
1 个回答

filePath需要转换为沙箱文件路径,文件操作转换参考:

https://developer.huawei.com/consumer/cn/doc/harmonyos-faqs/faqs-core-file-kit-0000001816452269

参考示例:

let uris: Array<string> = [];
const photoViewPicker = new picker.PhotoViewPicker();
const photoSelectOptions = new picker.PhotoSelectOptions();
photoSelectOptions.maxSelectNumber = 1; // 选择媒体文件的最大数目
photoViewPicker.select(photoSelectOptions).then((photoSelectResult: picker.PhotoSelectResult) => {
  uris = photoSelectResult.photoUris;
  console.info(‘photoViewPicker.select to file succeed and uris are:’ + uris);
  let file = fs.openSync(uris[0], fs.OpenMode.READ_ONLY);

  let file2 = fs.openSync(this.filesDir + ‘/test.jpg’, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE);//先创建有读写权限的文件,再把不可读写的文件复制过来
  fs.copyFileSync(file.fd, file2.fd);
  // 关闭文件
  fs.closeSync(file);
  fs.closeSync(file2);

}).catch((err: BusinessError) => {
  console.error(Invoke photoViewPicker.select failed, code is ${err.code}, message is ${err.message});
})
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
logo
HarmonyOS
子站问答
访问
宣传栏