HarmonyOS 获取照片的拍照时间或者修改时间?

请问使用picker获取照片的uri后,如何通过uri获取照片的拍照时间或者修改时间。

尝试使用awaiimage.createImageSource(destUri).getImageProperty()方法,不是返回401就是返回62980123,看官方文档这个api仅支持jpg,且图片必须拥有exif信息。

阅读 599
1 个回答

请参考:https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/js-apis-image-V5\#getimageproperties12

下述demo可以获取照片的拍照时间:

Button('打开相册')
  .onClick(() => {
    //创建图库选择器对象实例
    const photoViewPicker =new photoAccessHelper.PhotoViewPicker();
    //调用select()接口拉起图库界面进行文件选择,文件选择成功后,返回PhotoSelectResult结果集
    photoViewPicker.select().then(async (photoSelectResult: picker.PhotoSelectResult) => {
      //用一个全局变量存储返回的uri
      selectUris = photoSelectResult.photoUris;
      console.info('photoViewPicker.select to file succeed and uris are:' + selectUris);
    }).catch((err: BusinessError) => {
      console.error(`Invoke photoViewPicker.select failed, code is ${err.code}, message is ${err.message}`);
    })
  })

Button('creatPixelMap')
  .margin({ top: 10 })
  .onClick((event: ClickEvent) => {
    //使用fs.openSync接口,通过uri打开这个文件得到fd
    let file = fs.openSync(selectUris[0], fs.OpenMode.READ_ONLY);
    console.info('file fd: ' + file.fd);
    //根据文件fd创建imagSource
    const imageSource: image.ImageSource = image.createImageSource(file.fd);
    let key = [image.PropertyKey.IMAGE_WIDTH, image.PropertyKey.IMAGE_LENGTH, image.PropertyKey.DATE_TIME_ORIGINAL];
    imageSource.getImageProperties(key).then((data) => {
      console.info(JSON.stringify(data));
    }).catch((err: BusinessError) => {
      console.error(JSON.stringify(err));
    });
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进