HarmonyOS 实现点击按钮可以访问本地相册并能够选择图片的demo

HarmonyOS
3天前
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
shlp
import { picker } from '@kit.CoreFileKit';
import photoAccessHelper from '@ohos.file.photoAccessHelper';
import { BusinessError } from '@kit.BasicServicesKit';

@Entry
@Component
struct Index9 {
  @State selectedUri: string = '';

  choosePhoto() {
    try {
      let PhotoSelectOptions = new photoAccessHelper.PhotoSelectOptions();
      PhotoSelectOptions.MIMEType = photoAccessHelper.PhotoViewMIMETypes.IMAGE_TYPE;
      PhotoSelectOptions.maxSelectNumber = 5;
      PhotoSelectOptions.isEditSupported = true;
      PhotoSelectOptions.isPhotoTakingSupported = true;
      let photoPicker = new picker.PhotoViewPicker()
      let uris: Array<string> = [];
      photoPicker.select(PhotoSelectOptions).then((PhotoSelectResult: photoAccessHelper.PhotoSelectResult) => {
        console.info('PhotoViewPicker.select successfully, PhotoSelectResult uri: ' + JSON.stringify(PhotoSelectResult));
        uris = PhotoSelectResult.photoUris;
        this.selectedUri = uris[0];
      }).catch((err: BusinessError) => {
        console.error(`PhotoViewPicker.select failed with err: ${err.code}, ${err.message}`);
      });
    } catch (error) {
      let err: BusinessError = error as BusinessError;
      console.error(`PhotoViewPicker failed with err: ${err.code}, ${err.message}`);
    }
  }

  build() {
    Row() {
      Column({ space: 30 }) {
        Button('选择图片')
          .width(200)
          .height(30)
          .onClick(() => {
            this.choosePhoto()
          })
        Image(this.selectedUri)
          .width('95%')
          .height(400)
          .padding({top:'30xp'})
      }
      .width('100%')
      .height('100%')
    }
    .height('100%')
  }
}
分享
微博
QQ
微信
回复
3天前
相关问题
拉起相册选择图片页面失败
1950浏览 • 1回复 待解决
HarmonyOS 点击图片按钮中间没反应
683浏览 • 1回复 待解决
如何实现按钮点击效果?
465浏览 • 2回复 待解决
进入相册或拍照选择图片做头像
13602浏览 • 2回复 已解决
如何保存本地图片相册
1087浏览 • 1回复 待解决
HarmonyOS 图片选择器怎么实现
52浏览 • 1回复 待解决