HarmonyOS 使用picker打开文件选择器选择文件完成后如何获取到文件大小?

如果我选择的不是图片视频而是word之类的怎么获取到文件大小

阅读 410
1 个回答

在拿到uri后调用file接口获取文件大小,详情见:https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/photoaccesshelper-photoviewpicker-V5\#指定uri读取文件数据

let uris: Array<string> = [];
const photoViewPicker = new photoAccessHelper.PhotoViewPicker();
photoViewPicker.select(photoSelectOptions).then((photoSelectResult: photoAccessHelper.PhotoSelectResult) => {
  uris = photoSelectResult.photoUris;
  console.info('photoViewPicker.select to file succeed and uris are:' + uris);
  let uri: string = '';
  let file = fileIo.openSync(uri, fileIo.OpenMode.READ_ONLY);
  console.info('file fd: ' + file.fd);
  let buffer = new ArrayBuffer(4096);
  let readLen = fileIo.readSync(file.fd, buffer);
  console.info('readSync data to file succeed and buffer size is:' + readLen);
}).catch((err: BusinessError) => {
  console.error(`Invoke photoViewPicker.select failed, code is ${err.code}, message is ${err.message}`);
})
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
logo
HarmonyOS
子站问答
访问
宣传栏