HarmonyOS 关于使用picker读写媒体文件的问题?

需要自定义相册,但是不想去申请相关权限,目前只能通过PhotoViewPicker.select进行模拟。

1、PhotoViewPicker.select是使用临时授权的方式,退出后台重新进入是否会有影响?

2、通过PhotoViewPicker.select获取文件uri,需要获取文件相关信息,视频获取时长,缩略图等,图片获取宽高等信息。

3、视频需要进行压缩。

4、视频和图片都需要进行上传。

阅读 557
1 个回答

1、没有影响。

2、获取图片相关信息:请参考:https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/js-apis-image-V5\#getimageproperty11

demo:

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);
    //完成后关闭fd
    fs.closeSync(file);
    let decodingOptions: image.DecodingOptions = {
      editable: true,
      desiredPixelFormat: 3,
    }
    //创建pixelMap
    imageSource.createPixelMap(decodingOptions).then(async (pixelMap1: image.PixelMap) => {
      this.pixelMap = pixelMap1;
    });
  }

获取视频相关信息:获取视频时长,请参考:https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/js-apis-media-V5\#fetchmetadata11

获取视频缩略图,请参考:https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/avimagegenerator-V5

3、使用三四方库ohos\_videocompressor对视频进行压缩,请参考:https://gitee.com/openharmony-sig/ohos\_videocompressor\#ohos\_videocompressor

4、使用应用文件上传下载功能,请参考:https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/app-file-upload-download-V5应用文件上传下载支持沙箱路径,需要将相册中的文件复制到沙箱中,再行进行文件上传。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进