鸿蒙学习 - 相册图片选择 picker.PhotoViewPicker()

174 阅读1分钟

鸿蒙学习 - 相册图片选择 picker.PhotoViewPicker()

import common from '@ohos.app.ability.common'
import picker from '@ohos.file.picker'

@Entry
@Component
struct Index {
  private context = getContext(this) as common.UIAbilityContext
  @State url: string = ""
  build() {
    Column(){

    Button("测试按钮").onClick(() => {

      // 创建选择图片的参数
      let photoOptions = new picker.PhotoSelectOptions;
      photoOptions.MIMEType = picker.PhotoViewMIMETypes.IMAGE_TYPE;
      photoOptions.maxSelectNumber = 1;
      // 选择图片的组件
      let photoPicker = new picker.PhotoViewPicker()
      // 调用选择图片
      photoPicker.select(photoOptions).then(res => {
        this.url = res.photoUris[0] // 获取选中的图片地址
      })

    })
      Image(this.url).width(200) // 把选择的图片展示出来
    }
  }
}