如何解决photoAccessHelper.getAssets获取竖向视频宽高不正确问题
============================================

#### 问题现象

图库显示宽高1080\*1920。

![](https://contentcenter-vali-drcn.dbankcdn.cn/pvt_2/DeveloperAlliance_scene_100_1/2f/v3/b2cMaQ8nR9qBYAk2RxHPGA/zh-cn_image_0000002659258335.png?HW-CC-KV=V1&HW-CC-Date=20260709T134133Z&HW-CC-Expire=86400&HW-CC-Sign=47A5658D354C3C66DCD468916884CD447EF3F7FD4AE1288DD4B5D79333FD7A6C "点击放大")

通过API获取到的视频高为1080，宽为1920，API获取的值宽高与实际相反。

![](https://contentcenter-vali-drcn.dbankcdn.cn/pvt_2/DeveloperAlliance_scene_100_1/ed/v3/71SQX3vZSI2Z27z4uIrOyA/zh-cn_image_0000002628899116.png?HW-CC-KV=V1&HW-CC-Date=20260709T134133Z&HW-CC-Expire=86400&HW-CC-Sign=178E2D5C062E67E00C6327A07C4B925AA885C1E477E11B46307B64212CF418CC "点击放大")

#### 背景知识

*   使用[PhotoAccessHelper.getAssets](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/arkts-apis-photoaccesshelper-photoaccesshelper#getassets-1)获取媒体资源。
*   使用[PhotoAsset.get](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/arkts-apis-photoaccesshelper-photoasset#get)获取图片和视频文件关键信息。

#### 问题定位

获取横屏视频做对比，两个视频在手机上显示的尺寸宽高数值相反。

![](https://contentcenter-vali-drcn.dbankcdn.cn/pvt_2/DeveloperAlliance_scene_100_1/65/v3/gWioWTP2S_Gz16anACcOFw/zh-cn_image_0000002659138385.png?HW-CC-KV=V1&HW-CC-Date=20260709T134133Z&HW-CC-Expire=86400&HW-CC-Sign=97E27500037742FCFB751F32F8E641FA73A34628AEC263691BD2B145F49AC3FB "点击放大")

通过API获取到的两个视频同样高为1080，宽为1920，但是竖向视频有90°旋转，因此怀疑是旋转导致的问题。

![](https://contentcenter-vali-drcn.dbankcdn.cn/pvt_2/DeveloperAlliance_scene_100_1/a1/v3/NOl-0ePSRDmkf1To2dO8Qg/zh-cn_image_0000002629059034.png?HW-CC-KV=V1&HW-CC-Date=20260709T134133Z&HW-CC-Expire=86400&HW-CC-Sign=0A1A71F7249236061C940DAA0599ED5B5CBA8658EE73CF5B6BD4CBB286A471C9 "点击放大")

再对比下旋转180°和270°的情况，旋转度数除以180余数为90的时候，需要长宽的数值互换，才能得出手机上看到的结果。

![](https://contentcenter-vali-drcn.dbankcdn.cn/pvt_2/DeveloperAlliance_scene_100_1/b6/v3/DyQkbZNERii-TkJHccLSpQ/zh-cn_image_0000002659258337.png?HW-CC-KV=V1&HW-CC-Date=20260709T134133Z&HW-CC-Expire=86400&HW-CC-Sign=DC1E2D6D32ECDD86F5DFFC9F74A3747646C89788C3F2B252E3547DDC14AAECA8 "点击放大")

![](https://contentcenter-vali-drcn.dbankcdn.cn/pvt_2/DeveloperAlliance_scene_100_1/88/v3/9Olw1LV1S_idw072dZ5yUQ/zh-cn_image_0000002628899118.png?HW-CC-KV=V1&HW-CC-Date=20260709T134133Z&HW-CC-Expire=86400&HW-CC-Sign=1710319667A68D7FA7555C80912EF5BB1721CA252DFF14B3A667D76DC4B8CFD3 "点击放大")

#### 分析结论

获取视频或图片长宽时，还需要判断是否有旋转，如果旋转度数除以180余数为90即视频是竖屏时，需要长宽的数值互换，才能得出手机上看到的结果。

#### 修改建议

通过photoAsset.get('orientation')判断视频是否旋转，来判断视觉效果上的长宽和参数的对应关系：

import { photoAccessHelper } from '@kit.MediaLibraryKit';
import { dataSharePredicates } from '@kit.ArkData';
import { common } from '@kit.AbilityKit';
import { BusinessError } from '@kit.BasicServicesKit';

@Entry
@Component
struct PhAccessHelperGetAssets {
  @State phName: string \= '';
  @State phHeight: number \= 0;
  @State phWidth: number \= 0;
  @State phOrientation: number \= 0;

  async testMethod() {
    const photoSelectOptions \= new photoAccessHelper.PhotoSelectOptions();
    photoSelectOptions.MIMEType \= photoAccessHelper.PhotoViewMIMETypes.IMAGE\_VIDEO\_TYPE; _// 过滤选择媒体文件类型为IMAGE_
    photoSelectOptions.maxSelectNumber \= 1; _// 选择媒体文件的最大数目_
    let uris: Array<string\> = \[\];
    const photoViewPicker \= new photoAccessHelper.PhotoViewPicker();
   _ // 拉起相册_
    photoViewPicker.select(photoSelectOptions).then(async (photoSelectResult: photoAccessHelper.PhotoSelectResult) \=\> {
      uris \= photoSelectResult.photoUris;
      console.info('photoViewPicker.select to file succeed and uris are:' \+ uris);
      if (uris.length \=== 0) {
        return;
      }
      let context: Context \= this.getUIContext().getHostContext() as common.UIAbilityContext;
      let phAccessHelper \= photoAccessHelper.getPhotoAccessHelper(context);
      let predicates: dataSharePredicates.DataSharePredicates \= new dataSharePredicates.DataSharePredicates();
      let uri \= uris\[0\]; _// 需保证此uri已存在_
      predicates.equalTo(photoAccessHelper.PhotoKeys.URI, uri.toString());
      let fetchOptions: photoAccessHelper.FetchOptions \= {
        fetchColumns: \[
          'title',
          photoAccessHelper.PhotoKeys.WIDTH, _// 图片宽度_
          photoAccessHelper.PhotoKeys.HEIGHT, _// 图片高度_
          photoAccessHelper.PhotoKeys.DURATION, _// 持续时间_
          photoAccessHelper.PhotoKeys.SIZE, _//_ _文件大小_
          photoAccessHelper.PhotoKeys.DISPLAY\_NAME, _// 显示名字_
          photoAccessHelper.PhotoKeys.ORIENTATION, _//_ _图片文件的方向_
          photoAccessHelper.PhotoKeys.PHOTO\_SUBTYPE\], _//_ _媒体文件的子类型_
        predicates: predicates
      };
      let fetchResult: photoAccessHelper.FetchResult<photoAccessHelper.PhotoAsset\> =
        await phAccessHelper.getAssets(fetchOptions);
      let photoAsset: photoAccessHelper.PhotoAsset \= await fetchResult.getFirstObject();
      let title: photoAccessHelper.PhotoKeys \= photoAccessHelper.PhotoKeys.TITLE;
      let paTitle \= photoAsset.get(title.toString());
      this.phName \= paTitle.toString();
      this.phOrientation \= photoAsset.get('orientation') as number; _// 获取旋转角度_
      if (this.phOrientation % 180 \== 90) {
        this.phName \= paTitle.toString();
        this.phWidth \= photoAsset.get('height') as number;
        this.phHeight \= photoAsset.get('width') as number;
        this.phOrientation \= photoAsset.get('orientation') as number;
      } else {
        this.phWidth \= photoAsset.get('width') as number;
        this.phHeight \= photoAsset.get('height') as number;
      }
     _ // 释放FetchResult实例并使其失效_
      fetchResult.close();
      return photoAsset;
    }).catch((err: BusinessError) \=\> {
      console.error(\`Invoke photoViewPicker.select failed, code is ${err.code}, message is ${err.message}\`);
    });
    return;
  }

  build() {
    Column() {
      Button('选择图片或视频')
        .id('PhAccessHelperGetAssetsHelloWorld')
        .fontSize(30)
        .fontWeight(FontWeight.Bold)
        .alignRules({
          center: { anchor: '\_\_container\_\_', align: VerticalAlign.Center },
          middle: { anchor: '\_\_container\_\_', align: HorizontalAlign.Center }
        })
        .onClick(() \=\> {
          this.testMethod();
        });
      Text(\`图片：${this.phName},属性长：${this.phHeight}宽：${this.phWidth}角度：${this.phOrientation}\`);
    }
    .height('100%')
    .width('100%')
    .alignItems(HorizontalAlign.Center)
    .justifyContent(FlexAlign.Center);
  }
}