HarmonyOS 选择相册中图片达上限后toast提示

想要实现以下功能:唤起相册时,限制相册中的图片最大选择数量为3张,当选择第4张时,弹出toast提示‘数量已达上限’,请问是否支持呢?

HarmonyOS
1天前
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
shlp

可以通过maxSelectNumber选择图片数量,参考DEMO:

import { fileIo, picker } from '@kit.CoreFileKit'
import { BusinessError } from '@ohos.base';
import { image } from '@kit.ImageKit';
import { util } from '@kit.ArkTS';

@Entry
@Component
struct PackingImagePage {
  @State getImg: string = '获取图片'
  @State imgArray: string [] = []
  @State maxSelectNumber: number = 0

  async beginScan(url: string) {
    const tempPath = getContext().filesDir + "/" + util.generateRandomUUID() + ".jpg"
    const file = fileIo.openSync(url, fileIo.OpenMode.READ_ONLY);
    fileIo.copyFileSync(file.fd, tempPath);
    return tempPath;
  }

  getPhotoImage() {
    let photoPicker = new picker.PhotoViewPicker();
    photoPicker.select({
      MIMEType: picker.PhotoViewMIMETypes.IMAGE_TYPE,
      maxSelectNumber: 3
    }, async (err, data) => {
      if (err) {
        console.info("photopicker:" + err.message)
        return
      }
      this.imgArray = data.photoUris
      let tempPath = await this.beginScan(data.photoUris[0])
      const imagePackerApi: image.ImagePacker = image.createImagePacker();
      const imageSourceApi: image.ImageSource = image.createImageSource(tempPath);
      console.log(`${imageSourceApi}`)
      let packOpts: image.PackingOption = { format: "image/jpeg", quality: 98 };
      imagePackerApi.packing(imageSourceApi, packOpts, (err: BusinessError, data: ArrayBuffer) => {
        if (err) {
          console.log(`${err}`)
          console.error('packing failed.');
        } else {
          console.log(`${data.byteLength}`)
          console.info('packing succeeded.');
        }
      })
    })

  }

  build() {
    Column() {
      ForEach(this.imgArray, (item: string, index: number) => {
        Row() {
          Image(item).width(100).height(100).borderWidth(1)
        }
      })
      Button(this.getImg).margin(20)
        .onClick(() => {
          this.getPhotoImage()
        })
    }
    .width('100%')
    .height('100')
    .justifyContent(FlexAlign.Center)
  }
}
分享
微博
QQ
微信
回复
1天前
相关问题
拉起相册选择图片页面失败
1970浏览 • 1回复 待解决
HarmonyOS Toast 消息提示
297浏览 • 1回复 待解决
HarmonyOS 关于从相册选择图片上传
42浏览 • 1回复 待解决
进入相册或拍照选择图片做头像
13633浏览 • 2回复 已解决
HarmonyOS 如果显示类似Toast提示
418浏览 • 2回复 待解决
HarmonyOS 扩展通知消息未触问题
363浏览 • 1回复 待解决
如何读取相册图片
941浏览 • 1回复 待解决