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

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

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
相关问题
分享沙箱文件,应用可分享单个文件
455浏览 • 1回复 待解决
如何打开指定文件夹,选择文件返回
7304浏览 • 1回复 待解决
应用文件分享功能介绍
462浏览 • 0回复 待解决
ArkTS应用文件分享报错,code:16000050
625浏览 • 1回复 待解决
AVRecorder 支持的文件类型
354浏览 • 1回复 待解决
拉起相册选择图片页面失败
394浏览 • 1回复 待解决
怎样让用户选择并加载图片
7343浏览 • 1回复 待解决
创建PolarDB实例如何选择网络类型
1785浏览 • 1回复 待解决
如何选择PolarDB迁移类型和迁移对象?
1885浏览 • 1回复 待解决
如何将图片文本视频分享给其他应用
364浏览 • 1回复 待解决
进入相册或拍照选择图片做头像
12010浏览 • 2回复 已解决
gamePlayerId和teamPlayerId怎么选择
531浏览 • 1回复 待解决