HarmonyOS 无法实现在分享方调起分享弹窗中显示被分享方应用的icon?

该如何配置才能实现在分享方调起的分享弹窗中显示被分享目标应用的icon?

  1. 根据API文档【目标应用处理分享内容】配置无法实现在分享方调起分享弹窗中显示被分享方应用的icon。目标应用处理分享内容API:https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/share-interface-description-V5
  2. 且在应用配置文件(src/main/module.json5)的skills配置中注册,配置actions为ohos.want.action.sendData;uris中添加了.jpg支持的数据类型。
  3. 在系统图库应用中选中一张jpg格式的图片,点击分享;分享弹窗中未能如预期一样显示被分享的目标应用icon。
阅读 497
1 个回答

参考下此demo看能否实现效果,注意将图片路径改成自己的路径:

import common from '@ohos.app.ability.common';
import uniformTypeDescriptor from '@ohos.data.uniformTypeDescriptor';
import systemShare from '@hms.collaboration.systemShare';
import promptAction from '@ohos.promptAction';

let storage = LocalStorage.getShared()
const EVENT_BACK_PRESS = 'EVENT_BACK_PRESS'

@Entry(storage)
@Component
struct Index {
  private context = getContext(this) as common.UIAbilityContext
  @State selectionMode: systemShare.SelectionMode = systemShare.SelectionMode.SINGLE;
  @State previewMode: systemShare.SharePreviewMode = systemShare.SharePreviewMode.DEFAULT;
  @State anchorType: 'id' | 'rect' = 'rect';
  @LocalStorageLink('viewId') viewId: string = "";

  build() {
    Stack() {
      Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
        Button('分享 - 文本').margin({bottom:10})
          .onClick(() => {
            this.shareContent('hello word', this.context)
          })
        Button('分享 - 网站').margin({bottom:10})
          .onClick(() => {
            this.shareWebSite(this.context, 'www.harmonyos.com', 'HarmonyOS')
          })
        Button('分享 - 单个图片').margin({bottom:10})
          .onClick(() => {
            //将路径改成自己图片的路径
            this.sharePicture('file://com.example.MyA/data/local/tmp/getScreenSize.jpeg', this.context)
          })
      }
    }.width('100%').height('100%')
  }

  // 分享文本
  shareContent(content: string, context: common.UIAbilityContext): void {
    const record = {
      utd: uniformTypeDescriptor.UniformDataType.PLAIN_TEXT,
      content: content,
    } as systemShare.SharedRecord;
    this.huaweiShare(record, context)
  }

  // 分享图片
  sharePicture(uri: string, context: common.UIAbilityContext): void {
    const record = {
      utd: uniformTypeDescriptor.UniformDataType.IMAGE,
      uri: uri,
    } as systemShare.SharedRecord;
    this.huaweiShare(record, context);
  }

  // 分享网站
  shareWebSite(context: common.UIAbilityContext, url: string, title: string, thumbnail?: Uint8Array): void {
    const record = {
      utd: uniformTypeDescriptor.UniformDataType.HYPERLINK,
      content: url,
      title: title,
      description: url,
      thumbnail: thumbnail!,
    } as systemShare.SharedRecord;
    this.huaweiShare(record, context)
  }

  private huaweiShare(record: systemShare.SharedRecord, context: common.UIAbilityContext): void {
    try {
      let data = new systemShare.SharedData(record);
      let controller: systemShare.ShareController = new systemShare.ShareController(data);
      controller.on('dismiss', () => {
        promptAction.showToast({
          message: 'Share panel disappeared'
        });
      });
      controller.show(context, {
        previewMode: systemShare.SharePreviewMode.DETAIL,
        selectionMode: systemShare.SelectionMode.SINGLE
      });
    } catch (e) {
      const error = e as Error;
      console.error('share error: ' + error.message);
    }
  }

  onBackPress(): boolean {
    this.context.eventHub.emit(EVENT_BACK_PRESS)
    return true
  }
}
logo
HarmonyOS
子站问答
访问
宣传栏