HarmonyOS photopicker的demo

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

demo如下:

import photoAccessHelper from '@ohos.file.photoAccessHelper';
import {
  PhotoPickerComponent,
  PickerController,
  PickerOptions,
  DataType,
  ItemInfo,
  PhotoBrowserInfo,
  AnimatorParams,
  MaxSelected,
  ItemType,
  ClickType,
  PickerOrientation,
  SelectMode,
  PickerColorMode,
  ReminderMode,
  MaxCountType,
} from '@ohos.file.PhotoPickerComponent';
import { AlbumPickerComponent, AlbumPickerOptions, AlbumInfo } from '@ohos.file.AlbumPickerComponent';


@Entry
@Component
struct Index {
  @State pickerController: PickerController = new PickerController();
  pickerOptions: PickerOptions = new PickerOptions();
  pickerOptions1: PickerOptions = new PickerOptions();
  pickerOptions2: PickerOptions = new PickerOptions();
  albumOptions: AlbumPickerOptions = new AlbumPickerOptions();
  @State selectedUris: Array<string> = new Array<string>();
  @State allBackGroundColor: number = 0xf1f3f5;
  @State videoBackGroundColor: number = 0xffffff;
  @State photoBackGroundColor: number = 0xffffff;
  @State isShowAll: boolean = true;
  @State isShowVideo: boolean = false;
  @State isShowPhoto: boolean = false;
  @State Width: string = '100%';
  @State Height: string = '87%';
  @State isShowAlbum: boolean = false;
  private isBlock: boolean = false;
  @State isInPhotoBrowser: boolean = false;

  // onBackPress(): boolean | void {
  // return true;
  // }

  private onEnterPhotoBrowser(photoBrowserInfo: PhotoBrowserInfo): boolean {
    this.isInPhotoBrowser = true;
    console.info('1111111onEnterPhotoBrowser' + JSON.stringify(photoBrowserInfo));
    return false;
  }

  private onExitPhotoBrowser(photoBrowserInfo: PhotoBrowserInfo): boolean {
    this.isInPhotoBrowser = false;
    console.info('1111111onExitPhotoBrowser' + JSON.stringify(photoBrowserInfo));
    return false;
  }

  private onPickerControllerReady(): void {
    console.info('1111111onPickerControllerReady');
  }

  private onAlbumClick(albumInfo: AlbumInfo): boolean {
    this.isShowAlbum = false;
    console.info('1111111onAlbumClick' + JSON.stringify(albumInfo?.uri));
    if (albumInfo?.uri) {
      this.pickerController.setData(DataType.SET_ALBUM_URI, albumInfo.uri);
      console.info('1111111onAlbumClick' + JSON.stringify(albumInfo.uri));
    }
    return true;
  }

  private onSelect(uri: string): void {
    if (uri) {
      this.selectedUris.push(uri)
    }
    this.pickerOptions.preselectedUris = [...this.selectedUris];
    this.pickerOptions1.preselectedUris = [...this.selectedUris];
    this.pickerOptions2.preselectedUris = [...this.selectedUris];
    console.info('1111111onSelect1111' + JSON.stringify(this.selectedUris));
  }

  private onDeselect(uri: string): void {
    if (uri) {
      this.selectedUris = this.selectedUris.filter((item: string) => {
        return item !== uri;
      })
    }
    this.pickerOptions.preselectedUris = [...this.selectedUris];
    this.pickerOptions1.preselectedUris = [...this.selectedUris];
    this.pickerOptions2.preselectedUris = [...this.selectedUris];
    console.info('1111111onDeselect1111' + JSON.stringify(this.selectedUris));
  }

  private onItemClicked(itemInfo: ItemInfo, clickType: ClickType): boolean {
    if (!itemInfo) {
      return false;
    }
    let type: ItemType | undefined = itemInfo.itemType;
    let uri: string | undefined = itemInfo.uri;
    if (type === ItemType.CAMERA) {
      console.info('1111111onCameraClick');
      if (this.isBlock) {
        return false;
      }
    } else if (type === ItemType.THUMBNAIL) {
      if (clickType === ClickType.SELECTED) {
        if (this.isBlock) {
          return false;
        }
        if (uri) {
          this.selectedUris.push(uri)
          this.pickerOptions.preselectedUris = [...this.selectedUris];
          this.pickerOptions1.preselectedUris = [...this.selectedUris];
          this.pickerOptions2.preselectedUris = [...this.selectedUris];
        }
      } else {
        if (uri) {
          this.selectedUris = this.selectedUris.filter((item: string) => {
            return item !== uri;
          })
          this.pickerOptions.preselectedUris = [...this.selectedUris];
          this.pickerOptions1.preselectedUris = [...this.selectedUris];
          this.pickerOptions2.preselectedUris = [...this.selectedUris];
        }
      }
    } else {
    }
    console.info('1111111onItemClicked1111' + JSON.stringify(itemInfo));
    return true;
  }

  aboutToAppear() {
    this.pickerOptions.MIMEType = photoAccessHelper.PhotoViewMIMETypes.IMAGE_VIDEO_TYPE;
    this.pickerOptions.maxSelectNumber = 10;
    this.pickerOptions.isSearchSupported = true;
    this.pickerOptions.isPhotoTakingSupported = true;
    this.pickerOptions.checkBoxColor = '#E47833';
    this.pickerOptions1.MIMEType = photoAccessHelper.PhotoViewMIMETypes.VIDEO_TYPE;
    this.pickerOptions1.maxSelectNumber = 10;
    this.pickerOptions1.isSearchSupported = false;
    this.pickerOptions1.isPhotoTakingSupported = false;
    // this.pickerOptions1.checkBoxColor = '#E47833';
    this.pickerOptions2.MIMEType = photoAccessHelper.PhotoViewMIMETypes.IMAGE_TYPE;
    this.pickerOptions2.maxSelectNumber = 10;
    this.pickerOptions2.isSearchSupported = false;
    this.pickerOptions2.isPhotoTakingSupported = false;
    this.albumOptions.themeColorMode = PickerColorMode.AUTO;
    // this.pickerOptions2.checkBoxColor = '#E47833';
  }
  build() {
    Stack() {
      Column() {
        Row() {
          Button("拦截,返回值false").width('33%').height('5%').onClick(() => {
            this.isBlock = true;
          })
          Button("全部相册").width('33%').height('5%').onClick(() => {
            this.isShowAlbum = true;
          })
          Button("不拦截,返回值true").width('33%').height('5%').onClick(() => {
            this.isBlock = false;
          })
        }
        Row() {
          Button("全部").width('33%').height('8%')
            .onClick(() => {
              this.isShowAll = true;
              this.isShowVideo = false;
              this.isShowPhoto = false;
              this.pickerController.setData(DataType.SET_SELECTED_URIS, this.selectedUris);
            })
          Button("视频").width('33%').height('8%')
            .onClick(() => {
              this.isShowAll = false;
              this.isShowVideo = true;
              this.isShowPhoto = false;
              this.pickerController.setData(DataType.SET_SELECTED_URIS, this.selectedUris);
            })
          Button("图片").width('33%').height('8%')
            .onClick(() => {
              this.isShowAll = false;
              this.isShowVideo = false;
              this.isShowPhoto = true;
              this.pickerController.setData(DataType.SET_SELECTED_URIS, this.selectedUris);
            })
        }

        Stack() {
          PhotoPickerComponent({
            pickerOptions: this.pickerOptions,
            onSelect: (uri: string): void => this.onSelect(uri),
            onDeselect: (uri: string): void => this.onDeselect(uri),
            onItemClicked: (itemInfo: ItemInfo, clickType: ClickType): boolean => this.onItemClicked(itemInfo, clickType),
            onEnterPhotoBrowser: (photoBrowserInfo: PhotoBrowserInfo): boolean => this.onEnterPhotoBrowser(photoBrowserInfo),
            onExitPhotoBrowser: (photoBrowserInfo: PhotoBrowserInfo): boolean => this.onExitPhotoBrowser(photoBrowserInfo),
            onPickerControllerReady: (): void => this.onPickerControllerReady(),
            pickerController: this.pickerController,
          }).height(this.Height).width(this.Width).visibility(this.isShowAll ? Visibility.Visible : Visibility.Hidden)
          PhotoPickerComponent({
            pickerOptions: this.pickerOptions1,
            onSelect: (uri: string): void => this.onSelect(uri),
            onDeselect: (uri: string): void => this.onDeselect(uri),
            onItemClicked: (itemInfo: ItemInfo, clickType: ClickType): boolean => this.onItemClicked(itemInfo, clickType),
            onEnterPhotoBrowser: (photoBrowserInfo: PhotoBrowserInfo): boolean => this.onEnterPhotoBrowser(photoBrowserInfo),
            onExitPhotoBrowser: (photoBrowserInfo: PhotoBrowserInfo): boolean => this.onExitPhotoBrowser(photoBrowserInfo),
            onPickerControllerReady: (): void => this.onPickerControllerReady(),
            pickerController: this.pickerController,
          }).height(this.Height).width(this.Width).visibility(this.isShowVideo ? Visibility.Visible : Visibility.Hidden)
          PhotoPickerComponent({
            pickerOptions: this.pickerOptions2,
            onSelect: (uri: string): void => this.onSelect(uri),
            onDeselect: (uri: string): void => this.onDeselect(uri),
            onItemClicked: (itemInfo: ItemInfo, clickType: ClickType): boolean => this.onItemClicked(itemInfo, clickType),
            onEnterPhotoBrowser: (photoBrowserInfo: PhotoBrowserInfo): boolean => this.onEnterPhotoBrowser(photoBrowserInfo),
            onExitPhotoBrowser: (photoBrowserInfo: PhotoBrowserInfo): boolean => this.onExitPhotoBrowser(photoBrowserInfo),
            onPickerControllerReady: (): void => this.onPickerControllerReady(),
            pickerController: this.pickerController,
          }).height(this.Height).width(this.Width).visibility(this.isShowPhoto ? Visibility.Visible : Visibility.Hidden)
        }
      }
      Row() {
        ForEach(this.selectedUris, (uri: string) => {
          Image(uri).height('10%').width('20%').onClick(() => {
            this.selectedUris = this.selectedUris.filter((item: string) => {
              return uri != item;
            })
            this.pickerController.setData(DataType.SET_SELECTED_URIS, this.selectedUris);
            let maxSelect: MaxSelected = new MaxSelected();
            maxSelect.data = new Map<MaxCountType, number>([[MaxCountType.TOTAL_MAX_COUNT, 10], [MaxCountType.PHOTO_MAX_COUNT, 5], [MaxCountType.VIDEO_MAX_COUNT, 5]]);
            this.pickerController.setMaxSelected(maxSelect);
          })
        }, (uri: string) => JSON.stringify(uri))
      }

      if (this.isShowAlbum) {
        AlbumPickerComponent({
          albumPickerOptions: this.albumOptions,
          onAlbumClick: (albumInfo: AlbumInfo): boolean => this.onAlbumClick(albumInfo),
        }).height('100%').width('100%')
      }
    }
  }
}
分享
微博
QQ
微信
回复
1天前
相关问题
HarmonyOS 图像裁剪demo
327浏览 • 1回复 待解决
HarmonyOS 滑块认证demo
184浏览 • 1回复 待解决
HarmonyOS 音频会话demo
32浏览 • 1回复 待解决
HarmonyOS 上传文件demo
37浏览 • 1回复 待解决
HarmonyOS 页面跳转Demo
18浏览 • 1回复 待解决
HarmonyOS获取deviceiddemo
467浏览 • 1回复 待解决
HarmonyOS 搜索记录demo
45浏览 • 1回复 待解决
HarmonyOS 表单提交demo
44浏览 • 1回复 待解决
HarmonyOS Mqtt相关demo
54浏览 • 1回复 待解决
HarmonyOS 新闻相关DEMO参考
103浏览 • 1回复 待解决
HarmonyOS media.createAVPlayerdemo
29浏览 • 1回复 待解决
HarmonyOS 曲线demo
26浏览 • 1回复 待解决
HarmonyOS 手机蓝牙相关demo
67浏览 • 1回复 待解决
LocalSocket完整demo
409浏览 • 1回复 待解决
HarmonyOS 手势设置和校验demo
339浏览 • 1回复 待解决
HarmonyOS http网络请求封装Demo
28浏览 • 1回复 待解决
HarmonyOS 商城demo
34浏览 • 1回复 待解决
HarmonyOS web和原生交互demo
96浏览 • 1回复 待解决
HarmonyOS 应用导航设计完整demo
53浏览 • 1回复 待解决
HarmonyOS vue和ArkTS交互demo
24浏览 • 1回复 待解决