HarmonyOS 获取视频asset报错?

录制视频到沙箱目录中,然后使用photoAccessHelper.getPhotoAccessHelper API获取视频的缩略图等信息报错。

错误码为:14000011。

关键代码:

let fileUri = "/data/storage/el2/base/haps/entry/files/video_479.mp4";
let phAccessHelper = photoAccessHelper.getPhotoAccessHelper(this.context);
let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates();
// 配置查询条件,使用PhotoViewPicker选择图片返回的uri进行查询
predicates.equalTo('uri', fileUri);
let fetchOption: photoAccessHelper.FetchOptions = {
  fetchColumns: [photoAccessHelper.PhotoKeys.WIDTH, photoAccessHelper.PhotoKeys.HEIGHT,
    photoAccessHelper.PhotoKeys.TITLE, photoAccessHelper.PhotoKeys.DURATION,photoAccessHelper.PhotoKeys.DATE_ADDED,photoAccessHelper.PhotoKeys.PHOTO_TYPE,photoAccessHelper.PhotoKeys.SIZE],
  predicates: predicates
};
let fetchResult: photoAccessHelper.FetchResult<photoAccessHelper.PhotoAsset> =await phAccessHelper.getAssets(fetchOption);
阅读 593
1 个回答

可以将PhotoViewPicker.select接口得到的uri使用photoAccessHelper.getAssets接口获取对应uri的PhotoAsset对象。这种方式获取的对象可以调用getThumbnail获取缩略图和使用get接口读取PhotoKeys中的部分信息。

文档如下:https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/user-file-uri-intro-V5\#%E5%AA%92%E4%BD%93%E6%96%87%E4%BB%B6uri%E7%9A%84%E4%BD%BF%E7%94%A8%E6%96%B9%E5%BC%8F

import { common } from '@kit.AbilityKit';
import { fileUri, picker } from '@kit.CoreFileKit';
import fs from '@ohos.file.fs';
import { photoAccessHelper } from '@kit.MediaLibraryKit';
import { dataSharePredicates } from '@kit.ArkData';
import { image } from '@kit.ImageKit';
let uri:string = '';
@Entry
@Component
struct PhotoPage {
  @State imagePath: string = ''
  @State pixelMap:image.PixelMap  | undefined = undefined
  private controller:VideoController | undefined
  private context = getContext(this) as common.UIAbilityContext;

  build() {
    Row() {
      Column() {
        Video({
          src:this.imagePath,
          controller: this.controller
        }).height(300).width('100%').margin({ bottom: 20 })
        Button("选择视频")
          .onClick(() => {
            this.selectPhoto()
          })
        Image(this.pixelMap).width(300).height(400)
      }.width('100%')
    }.height('100%')
  }
  selectPhoto() {
    const photoSelectOptions = new picker.PhotoSelectOptions();
    const photoViewPicker = new picker.PhotoViewPicker();
    photoSelectOptions.MIMEType = picker.PhotoViewMIMETypes.VIDEO_TYPE; // 过滤选择媒体文件类型为VIDEO
    photoSelectOptions.maxSelectNumber = 1; // 选择媒体文件的最大数目
    photoViewPicker.select(photoSelectOptions).then((photoSelectResult: picker.PhotoSelectResult) => {
      const fileUri = photoSelectResult.photoUris[0]
      uri = fileUri
      this.getFileInfo(fileUri)
      this.uriGetAssets()
    })
  }
  async getFileInfo(filePathString: string) {
    let resFile = fs.openSync(filePathString, fs.OpenMode.READ_ONLY)
    const dateStr = (new Date().getTime()).toString()
    // 临时文件目录
    let newPath = this.context.filesDir + /${dateStr + resFile.name};
    // 转化路径
    fs.copyFileSync(resFile.fd, newPath);
    // 新的路径
    let realUri = 'file://' + newPath;
    this.imagePath = realUri
    console.log(this.imagePath)
  }

  async  uriGetAssets() {
    try {
      let phAccessHelper = photoAccessHelper.getPhotoAccessHelper(this.context);
      let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates();
      // 配置查询条件,使用PhotoViewPicker选择图片返回的uri进行查询
      predicates.equalTo('uri', uri);
      let fetchOption: photoAccessHelper.FetchOptions = {
        fetchColumns: [],
        predicates: predicates
      };
      let fetchResult: photoAccessHelper.FetchResult<photoAccessHelper.PhotoAsset> = await phAccessHelper.getAssets(fetchOption);
      // 得到uri对应的PhotoAsset对象,读取文件的部分信息
      const asset: photoAccessHelper.PhotoAsset = await fetchResult.getFirstObject();
      console.info('asset displayName: ', asset.displayName);
      console.info('asset uri: ', asset.uri);
      console.info('asset photoType: ', asset.photoType);
      console.info('asset width: ', asset.get(photoAccessHelper.PhotoKeys.WIDTH));
      console.info('asset height: ', asset.get(photoAccessHelper.PhotoKeys.HEIGHT));
      console.info('asset title: ' + asset.get(photoAccessHelper.PhotoKeys.TITLE));
      // 获取缩略图
      asset.getThumbnail((err, pixelMap) => {
        if (err == undefined) {
          this.pixelMap = pixelMap
          console.info('getThumbnail successful ' + JSON.stringify(pixelMap));
        } else {
          console.error('getThumbnail fail', err);
        }
      });
    } catch (error){
      console.error('uriGetAssets failed with err: ' + JSON.stringify(error));
    }
  }
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进