目录

系统分享与数据接收常见问题

Q:开发者在使用系统分享时,会遇到以下几个经典场景:

A:使用systemShare相关API前,需要先引入依赖模块,并获取UIAbilityContext上下文,后续各场景代码均基于以下导入和上下文:

import { systemShare } from '@kit.ShareKit';
import { fileIo, fileUri } from '@kit.CoreFileKit';
import { uniformTypeDescriptor as utd } from '@kit.ArkData';
import { common } from '@kit.AbilityKit';
import { BusinessError } from '@kit.BasicServicesKit';

@Entry
@Component
struct Index {
// 获取UIAbilityContext,用于访问应用文件目录和拉起分享面板
context: common.UIAbilityContext = this.getUIContext().getHostContext() as common.UIAbilityContext;

// ...
}

Q:使用Share Kit API分享图片,URI是本地图片地址,如何设置为http或者https开头的线上图片?

A:系统分享不支持分享远端图片;应先将网络图片保存至本地,获取路径后再进行分享。

Q:调用系统分享Share Kit后,没有分享的目标应用。

A:目标应用需要具备数据处理能力组件并在配置文件中声明所支持的分享能力,可参考目标应用处理分享内容。

Q:使用systemShare.getSharedData(want)获取分享的数据,报错1003703001,如何解决?

A:数据记录数量超过限制,可以不通过getSharedData接口获取分享数据,直接从want.uri属性,即分享的链接中,解析出分享的数据。

Q:Share Kit简介文档中"宿主应用配置分享数据描述信息总量不能超过200KB"是指分享的文件大小吗?

A:Share Kit简介文档中提到的200KB限制是指分享的数据描述信息的大小限制,而不是单个文件的大小限制。即通过Share Kit分享多个文件或大量数据时,需确保这些数据的总描述信息不超过200KB的限制。

Q:PC端用华为分享发一个文件到PAD端,PAD端接收后会自动打开文件预览,但是PAD端华为分享发给PC端一个文件,PC接收后显示的文件分享列表,并没有文件预览,这是系统规格吗?

A:是的,系统规格;其他设备将文件分享至PC时,文件默认保存至华为分享目录,不会直接打开;当分享到PAD上会直接预览打开。

Q:接入华为分享是否还需要再对接其他系统(如微信、微博等)的分享?

A:统一分享提供分享入口和面板,目标应用需要对能够接收的数据类型进行定义,宿主应用需要按照规范定义分享数据。接入统一分享后,系统自动识别该分享数据可分享至哪些目标应用,在分享面板中展示。无需开发者进行主动接入。具体流程可参考系统分享流程。系统分享的实现宿主应用和目标应用要分别遵守宿主应用开发规范和目标应用开发规范。

Q:使用系统的分享,怎么拿到结果回调。

A:可参考获取分享结果,注意从5.1.0(18)版本开始,支持宿主应用获取用户分享结果。

Q:系统分享"保存至图库"是否需要权限,按钮能否自定义?

A:系统分享保存图片到图库不需要额外申请权限,保证用户体验的一致性,无法自定义面板上的公共按钮。

Q:碰一碰分享是否可以跳过卡片分享页面直接分享数据?

A:碰一碰不支持直接分享数据,存在误触的可能性,极端情况会导致数据发送给非预期的设备。

Q:手机分享文件给PC设备,是否可以默认指定应用打开文件?

A:目前不支持,PC接收到文件后会存储在文件管理"华为分享"目录,默认用文件管理高亮显示接收到的文件。

Q:系统分享如何传递自定义参数?

A:可参考SharedData接口说明。

Q:如何区分通过隔空传送分享过来的数据和通过本地文件管理点击文件选择其他应用打开的数据?

A:隔空传送接收的数据不会直接发给应用,通常通过图库或者文件管理接收,最常用的是通过预览的方式拉起应用。

Q:如何实现自定义分享方式?

A:可参考半接模式分享,开发者自行开发分享能力面板,并在分享面板中提供系统分享入口。适用于分享方式区有商业诉求,或有自己独有的业务逻辑的开发者。

Q:分享PDF文件时,分享面板没有自己开发的应用,如何打开三方应用分享的PDF文件?

A:具体操作步骤如下:

  1. 目标应用注册支持分享内容的能力。在应用配置文件添加skills。
    {
      "module": {
        "name": "entry",
        "type": "entry",
        "description": "$string:module_desc",
        "mainElement": "EntryAbility",
        "deviceTypes": [
          "phone"
        ],
        "deliveryWithInstall": true,
        "installationFree": false,
        "pages": "$profile:main_pages",
        "abilities": [
          {
            "name": "EntryAbility",
            "srcEntry": "./ets/entryability/EntryAbility.ets",
            "description": "$string:EntryAbility_desc",
            "icon": "$media:layered_image",
            "label": "$string:EntryAbility_label",
            "startWindowIcon": "$media:startIcon",
            "startWindowBackground": "$color:start_window_background",
            "exported": true,
            "skills": [
              {
                "entities": [
                  "entity.system.home"
                ],
                "actions": [
                  "ohos.want.action.home"
                ]
              },
              // 添加skill配置
              {
                "actions": [
                  // 必需,声明数据处理能力
                  "ohos.want.action.viewData",
                  "ohos.want.action.sendData"
                ],
                "uris": [
                  {
                    "scheme": "file",
                    // 目标应用在配置支持接收的数据类型时,需穷举支持的UTD。
                    "utd": "com.adobe.pdf",
                    // maxFileSupported对于归属指定类型的文件,标识一次支持接收的最大数量。默认为0,代表不支持此类文件的分享。
                    "maxFileSupported": 1
                  },
                  {
                    "scheme": "file",
                    "utd": "general.object",
                    "maxFileSupported": 1
                  }
                ]
              }
            ]
          }
        ]
      }
    }
  2. 获取三方应用分享的内容。由于存在应用沙箱隔离,被分享的目标应用无法直接操作宿主应用沙箱中的文件,需拷贝文件至本应用沙箱。
    import { UIAbility, Want } from '@kit.AbilityKit';
    import { hilog } from '@kit.PerformanceAnalysisKit';
    import { window } from '@kit.ArkUI';
    import { systemShare } from '@kit.ShareKit';
    import { fileIo } from '@kit.CoreFileKit';
    import { BusinessError } from '@kit.BasicServicesKit';
    
    const DOMAIN = 0x0000;
    
    export default class EntryAbility extends UIAbility {
      onCreate(want: Want): void {
        hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onCreate');
        this.handleSharedData(want);
      }
    
      onNewWant(want: Want): void {
        this.handleSharedData(want);
      }
    
      onWindowStageCreate(windowStage: window.WindowStage): void {
        hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onWindowStageCreate');
    
        windowStage.loadContent('pages/Index', (err) => {
          if (err.code) {
            hilog.error(DOMAIN, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err));
            return;
          }
          hilog.info(DOMAIN, 'testTag', 'Succeeded in loading the content.');
        });
      }
    
      handleSharedData(want: Want) {
        systemShare.getSharedData(want)
          .then((data: systemShare.SharedData) => {
            data?.getRecords()?.forEach((record: systemShare.SharedRecord) => {
              if (!record.uri) {
                return;
              }
    
              try {
                // 获取uri,缓存至应用沙箱
                const file = fileIo.openSync(record.uri, fileIo.OpenMode.READ_ONLY);
                const destPath = this.context.cacheDir + '/' + file.name;
                const outFile = fileIo.openSync(destPath, fileIo.OpenMode.READ_WRITE | fileIo.OpenMode.CREATE);
                const shareFilePath = outFile.path;
                fileIo.copyFileSync(file.fd, outFile.fd);
                fileIo.closeSync(file);
                fileIo.closeSync(outFile);
                AppStorage.setOrCreate('SHARE_FILE_PATH', shareFilePath);
              } catch (error) {
                console.error(`Failed to parse share data. Code: ${error.code}, message: ${error.message}`);
              }
            });
          })
          .catch((error: BusinessError) => {
            console.error(`Failed to getSharedData. Code: ${error.code}, message: ${error.message}`);
          });
      }
    };
  3. 使用PdfView预览PDF文件。
    import { pdfService, PdfView, pdfViewManager } from '@kit.PDFKit';
    
    @Entry
    @Component
    struct Index {
      context = this.getUIContext().getHostContext()!;
      private controller: pdfViewManager.PdfController = new pdfViewManager.PdfController();
    
      build() {
        Column() {
          Button('预览')
            .onClick(() => {
              const path = AppStorage.get('SHARE_FILE_PATH') as string;
              const pdfDocument: pdfService.PdfDocument = new pdfService.PdfDocument();
              pdfDocument.loadDocument(path);
              this.controller.loadDocument(path);
            })
    
          PdfView({
            controller: this.controller,
            pageFit: pdfService.PageFit.FIT_WIDTH,
            showScroll: true
          })
            .id('pdfview_app_view')
            .layoutWeight(1)
        }
        .height('100%')
        .width('100%')
      }
    }

Q:为什么WPS移动版分享PDF文件看不到目标应用?

A:宿主应用在发起文件分享时,填写的UTD可能有差异,目标应用需穷举支持的UTD类型“general.object”。