目录

Sharing Videos

Video sharing allows users to share one or more videos to the target device or app.

Development Procedure

  1. Import required modules.

    import { systemShare } from '@kit.ShareKit';
    import { uniformTypeDescriptor as utd } from '@kit.ArkData';
    import { common } from '@kit.AbilityKit';
    import { fileUri } from '@kit.CoreFileKit';
    import { image } from '@kit.ImageKit';
    import { BusinessError } from '@kit.BasicServicesKit';
  2. (Recommended) Generate a video cover image.

    // Generate a video cover image.
    let uiContext: UIContext = this.getUIContext();
    let contextFaker: Context = uiContext.getHostContext() as Context;
    let thumbnailPath = contextFaker.filesDir + '/exampleImage.jpg'; // This is only an example. Replace it with the correct file path.
    let imageSource: image.ImageSource = image.createImageSource(thumbnailPath);
    let imagePacker: image.ImagePacker = image.createImagePacker();
    let buffer: ArrayBuffer = await imagePacker.packToData(imageSource, {
      // The value of format can only be image/jpeg, image/webp, or image/png.
      format: 'image/jpeg',
      // Quality of the output image in JPEG encoding. The value ranges from 0 to 100.
      // You are advised to compress the image to a reasonable size. An oversized image will fail to be shared.
      quality: 30
    });
  3. Construct the data to be shared.

    // Construct ShareData. You need to configure a valid data record.
    let filePath = contextFaker.filesDir + '/exampleVideo.mp4'; // This is only an example. Replace it with the correct file path.
    // Obtain the precise UTD type.
    let utdTypeId = utd.getUniformDataTypeByFilenameExtension('.mp4', utd.UniformDataType.VIDEO);
    let shareData: systemShare.SharedData = new systemShare.SharedData({
      utd: utdTypeId,
      uri: fileUri.getUriFromPath(filePath),
      title: 'Video title', // If this parameter is not passed, the video file name will be displayed.
      description: 'Video description', // If this parameter is not passed, the video size will be displayed.
      thumbnail: new Uint8Array(buffer) // You are advised to pass a suitable thumbnail for preview. If no thumbnail is passed, the first frame of the video will be used for preview by default.
    });

    You can use the fileUri.getUriFromPath method to obtain the file URI from the sandbox path.

  4. Add a record.

    shareData.addRecord({
      utd: utdTypeId,
      uri: fileUri.getUriFromPath(filePath),
      title: 'Video title', // If this parameter is not passed, the video file name will be displayed.
      description: 'Video description' // If this parameter is not passed, the video size will be displayed.
    });
  5. Start the sharing panel.

    // Display the sharing panel.
    let controller: systemShare.ShareController = new systemShare.ShareController(shareData);
    let context: common.UIAbilityContext = uiContext.getHostContext() as common.UIAbilityContext;
    controller.show(context, {
      selectionMode: systemShare.SelectionMode.SINGLE,
      previewMode: systemShare.SharePreviewMode.DETAIL
    }).then(() => {
      console.info('ShareController show success.');
    }).catch((error: BusinessError) => {
      console.error(`ShareController show error. code: ${error.code}, message: ${error.message}`);
    });

    For details about the complete sample code, please refer to Sample Code - Sharing Videos.