openharmony 鸿蒙 ohos-file-RecentPhotoComponent

2025-06-12 浏览 (1)

@ohos.file.RecentPhotoComponent (RecentPhotoComponent)

The RecentPhotoComponent embedded in the UI of an application allows the application to access the latest image or video in the user directory without the required permission. This component grants the application only the read permission.

NOTE

This component is supported since API version 12. Updates will be marked with a superscript to indicate their earliest API version.

Modules to Import

import {
  RecentPhotoComponent, RecentPhotoOptions, RecentPhotoCheckResultCallback,
  RecentPhotoClickCallback, PhotoSource, RecentPhotoInfo, RecentPhotoCheckInfoCallback,
} from '@ohos.file.RecentPhotoComponent';

Properties

The universal properties are supported.

RecentPhotoComponent

RecentPhotoComponent({ recentPhotoOptions?: RecentPhotoOptions, onRecentPhotoCheckResult?: RecentPhotoCheckResultCallback, onRecentPhotoClick: RecentPhotoClickCallback, onRecentPhotoCheckInfo?: RecentPhotoCheckInfoCallback, })

Allows the application to access the latest image or video in the user directory without the media access permission.

Decorator: @Component

System capability: SystemCapability.FileManagement.PhotoAccessHelper.Core

Parameters

NameTypeMandatoryDescription
recentPhotoOptionsRecentPhotoOptionsNoConfiguration of the latest image or video.
Atomic service API: This API can be used in atomic services since API version 12.
onRecentPhotoCheckResultRecentPhotoCheckResultCallbackNoCallback used to return the query result of the latest image or video.
Atomic service API: This API can be used in atomic services since API version 12.
onRecentPhotoClickRecentPhotoClickCallbackYesCallback to be invoked when the latest image or video is selected.
Atomic service API: This API can be used in atomic services since API version 12.
onRecentPhotoCheckInfo13+RecentPhotoCheckInfoCallbackNoCallback used to return information about the latest image or video obtained.
Atomic service API: This API can be used in atomic services since API version 13.

RecentPhotoOptions

Represents the configuration of the latest image or video.

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.FileManagement.PhotoAccessHelper.Core

NameTypeMandatoryDescription
periodnumberNoTime period for the latest image or video, in seconds. The maximum value is 86400 seconds (one day), which is also the default value.
If the value is less than or equal to 0, greater than 86400, or not set, the most recent photos over the longest period of up to one day is displayed by default. If there is no image or video in the specified period, the component is not displayed.
MIMETypephotoAccessHelper.PhotoViewMIMETypesNoTypes of the file displayed. The default value is PhotoViewMIMETypes.IMAGE_VIDEO_TYPE.
photoSourcePhotoSourceNoSource of the latest image or video, for example, photo or video taken by the camera or screenshot. By default, the source is not restricted.

RecentPhotoInfo13+

Represents information about the latest image or video.

Atomic service API: This API can be used in atomic services since API version 13.

System capability: SystemCapability.FileManagement.PhotoAccessHelper.Core

NameTypeMandatoryDescription
dateTakennumberNoTime when the latest image or video is taken, in ms. The value is the number of milliseconds elapsed since the Unix epoch (00:00:00 UTC on January 1, 1970).
identifierstringNoHash value of the name of the latest image or video, which is used to help the application determine whether the image or video to be displayed is the same as the one displayed before.

RecentPhotoCheckResultCallback

type RecentPhotoCheckResultCallback = (recentPhotoExists: boolean) => void

Called to return the query result of the latest image or video.

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.FileManagement.PhotoAccessHelper.Core

Parameters

NameTypeMandatoryDescription
recentPhotoExistsbooleanYesWhether the latest image or video exists. The value true means that the latest image or video exists, and false means the opposite. The default value is true.

RecentPhotoClickCallback

type RecentPhotoClickCallback = (recentPhotoInfo: BaseItemInfo) => boolean

Called when the latest image or video is selected.

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.FileManagement.PhotoAccessHelper.Core

Parameters

NameTypeMandatoryDescription
recentPhotoInfoBaseItemInfoYesInformation about the latest image or video.

Return value

TypeDescription
booleanProcessing result of the latest image or video. The value true means that the processing is complete.

RecentPhotoCheckInfoCallback13+

type RecentPhotoCheckInfoCallback = (recentPhotoExists: boolean, info: RecentPhotoInfo) => void

Called to return whether the latest image or video exists and the information about it.

Atomic service API: This API can be used in atomic services since API version 13.

System capability: SystemCapability.FileManagement.PhotoAccessHelper.Core

Parameters

NameTypeMandatoryDescription
recentPhotoExistsbooleanYesWhether the latest image or video exists. The value true means that the latest image or video exists, and false means the opposite. The default value is true.
infoRecentPhotoInfoYesInformation about the latest image or video.

PhotoSource

Enumerates the sources of the image or video data.

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.FileManagement.PhotoAccessHelper.Core

NameValueDescription
ALL0Images and videos from all sources.
CAMERA1Photo or video taken by the camera.
SCREENSHOT2Screenshot or screen capture video.

Example

// xxx.ets
import {
  photoAccessHelper
} from '@kit.MediaLibraryKit';
import {
  RecentPhotoComponent, RecentPhotoOptions, PhotoSource, RecentPhotoInfo, RecentPhotoCheckResultCallback, RecentPhotoClickCallback, RecentPhotoCheckInfoCallback
} from '@ohos.file.RecentPhotoComponent';
import {
  BaseItemInfo
} from '@ohos.file.PhotoPickerComponent';

@Entry
@Component
struct PickerDemo {
  private recentPhotoOptions: RecentPhotoOptions = new RecentPhotoOptions();
  private recentPhotoCheckResultCallback: RecentPhotoCheckResultCallback = (recentPhotoExists: boolean) => this.onRecentPhotoCheckResult(recentPhotoExists);
  private recentPhotoClickCallback: RecentPhotoClickCallback = (recentPhotoInfo: BaseItemInfo): boolean => this.onRecentPhotoClick(recentPhotoInfo);
  private recentPhotoCheckInfoCallback: RecentPhotoCheckInfoCallback = (recentPhotoExists: boolean, info: RecentPhotoInfo) => this.onRecentPhotoCheckInfo(recentPhotoExists, info);

  aboutToAppear() {
    this.recentPhotoOptions.MIMEType = photoAccessHelper.PhotoViewMIMETypes.IMAGE_VIDEO_TYPE;
    this.recentPhotoOptions.period = 30;
    this.recentPhotoOptions.photoSource = PhotoSource.ALL;
  }

  private onRecentPhotoCheckResult(recentPhotoExists: boolean): void {
    // Photo or video that meets the search criteria exists.
    if (recentPhotoExists) {
      console.info('The photo is exist.');
    }
  }

  private onRecentPhotoClick(recentPhotoInfo: BaseItemInfo): boolean {
    // Return the photo or video.
    if (recentPhotoInfo) {
      console.info('The photo uri is ' + recentPhotoInfo.uri);
      return true;
    }
    return true;
  }

  private onRecentPhotoCheckInfo(recentPhotoExists: boolean, info: RecentPhotoInfo): void {
    // Check whether a photo or video that meets the conditions exists. If yes, obtain information about the photo or video.
  }

  build() {
    Stack() {
      RecentPhotoComponent({
        recentPhotoOptions: this.recentPhotoOptions,
        onRecentPhotoCheckResult: this.recentPhotoCheckResultCallback,
        onRecentPhotoClick: this.recentPhotoClickCallback,
        onRecentPhotoCheckInfo: this.recentPhotoCheckInfoCallback,
      }).height('100%').width('100%')
    }
  }
}

你可能感兴趣的鸿蒙文章

harmony 鸿蒙Media Library Kit (Media File Management Service)

harmony 鸿蒙media_access_helper_capi.h

harmony 鸿蒙media_asset_base_capi.h

harmony 鸿蒙media_asset_capi.h

harmony 鸿蒙media_asset_change_request_capi.h

harmony 鸿蒙media_asset_manager_capi.h

harmony 鸿蒙MediaAssetManager

harmony 鸿蒙MediaLibrary_RequestId

harmony 鸿蒙MediaLibrary_RequestOptions

harmony 鸿蒙moving_photo_capi.h

  • 所属分类: 后端技术
  • 本文标签: 鸿蒙 软件
  • 版权声明: 本文链接 https://seaxiang.com/blog/M9ZAUk