多个文件分享,分享模式选择 - 多文件分享 - 文件类型选择(图片/视频/文件)- 发送选择文件/取消选择

分享模式选择 - 多文件分享 - 文件类型选择(图片/视频/文件)- 发送选择文件/取消选择

HarmonyOS
2024-05-20 22:00:05
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
FengTianYa

核心代码解释

import systemShare from '../api/systemShare'; 
import promptAction from '@ohos.promptAction'; 
import Logger from '../utils/Logger'; 
import { systemShareMockKit as shareMock, systemShareMockKit } from '../mock'; 
import common from '@ohos.app.ability.common'; 
import uniformTypeDescriptor from '@ohos.data.uniformTypeDescriptor'; 
import mediaquery from '@ohos.mediaquery'; 
import componentUtils from '@ohos.arkui.componentUtils'; 
// import ufm from '@ohos.filemanagement.userFileManager'; 
import dataSharePredicates from '@ohos.data.dataSharePredicates'; 
 
const storage = LocalStorage.getShared(); 
const logger = Logger.getLogger('Index'); 
 
@Entry(storage) 
@Component 
struct UIExtPreviewDemo { 
  @LocalStorageLink('context') context: common.UIAbilityContext = storage.get<common.UIAbilityContext>('context')!; 
  @State selectionMode: systemShare.SelectionMode = systemShare.SelectionMode.BATCH; 
  @State previewMode: systemShare.SharePreviewMode = systemShare.SharePreviewMode.DEFAULT; 
  anchor?: systemShare.ShareControllerAnchor; 
  @State anchorType: 'id' | 'rect' = 'rect'; 
  @State usePrototype: boolean = false; 
 
  @Builder 
  SelectionModeOption(name: string, mode: systemShare.SelectionMode) { 
    Row({ space: 5 }) { 
      Radio({ value: name, group: 'SelectionMode' }) 
        .onChange((checked: boolean) => { 
          if (checked) { 
            this.selectionMode = mode; 
          } 
        }) 
        .checked(this.selectionMode === mode) 
      Text(name) 
    } 
  } 
 
  build() { 
    Scroll() { 
 
      Column({ space: 10 }) { 
        Text('分享选择模式') 
        Row({ space: 10 }) { 
          this.SelectionModeOption('批量', systemShare.SelectionMode.BATCH) 
        } 
 
        Text('HarmonyOS 分享 Demo').fontSize(24) 
 
        if (this.selectionMode === systemShare.SelectionMode.BATCH) { 
          Button('批量 - 500') 
            .onClick(async () => { 
              let data = shareMock.createSharedData(await shareMock.sandBoxFile('image')); 
              for (let i = 0; i < 300; i++) { 
                data.addRecord(await shareMock.sandBoxFile('image')); 
              } 
              this.showSharePanel(data); 
            }) 
 
          Button('批量 - 多文件(包含文件夹、图片、视频、普通文件)') 
            .onClick(() => { 
              let data = shareMock.createSharedData(shareMock.userFile('folder'), shareMock.userFile('image'), 
                shareMock.userFile('video'), shareMock.userFile('text')); 
              this.showSharePanel(data); 
            }) 
        } 
 
      } 
      .alignItems(HorizontalAlign.Center) 
    } 
    .width('100%') 
    .height('100%') 
  } 
 
  private showSharePanel(data: systemShare.SharedData, albumInfo?: systemShare.AlbumInfo): void { 
    let controller = new systemShare.ShareController(data); 
    controller.on('dismiss', () => { 
      promptAction.showToast({ 
        message: 'Share panel disappeared' 
      }); 
    }); 
    logger.info('show start'); 
    if (this.usePrototype) { 
      controller 
        // @ts-ignore 
        .showForTest(this.context, { 
          selectionMode: this.selectionMode, 
          previewMode: this.previewMode, 
          albumInfo: albumInfo, 
          anchor: this.anchorType === 'id' ? 'anchor' : componentUtils.getRectangleById('anchor') 
          // @ts-ignore 
        }, 'com.huawei.share.prototype', 'SystemShareModalAbility'); 
    } else { 
      controller 
        .show(this.context, { 
          selectionMode: this.selectionMode, 
          previewMode: this.previewMode, 
          albumInfo: albumInfo, 
          anchor: this.anchorType === 'id' ? 'anchor' : componentUtils.getRectangleById('anchor') 
        }); 
    } 
    logger.info('show end'); 
  } 
}
分享
微博
QQ
微信
回复
2024-05-22 15:47:13
提问
该提问已有0人参与 ,帮助了0人