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

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

阅读 411
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
子站问答
访问
宣传栏