• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# @ohos.file.RecentPhotoComponent (RecentPhotoComponent)
2
3The **RecentPhotoComponent** component 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.
4
5> **NOTE**
6>
7> This component is supported since API version 12. Updates will be marked with a superscript to indicate their earliest API version.
8
9## Modules to Import
10
11```ts
12import {
13  RecentPhotoComponent, RecentPhotoOptions, RecentPhotoCheckResultCallback,
14  RecentPhotoClickCallback, PhotoSource
15} from '@ohos.file.RecentPhotoComponent';
16```
17
18## Properties
19
20The [universal properties](../apis-arkui/arkui-ts/ts-universal-attributes-size.md) are supported.
21
22## RecentPhotoComponent
23
24RecentPhotoComponent({
25  recentPhotoOptions?: RecentPhotoOptions,
26  onRecentPhotoCheckResult?: RecentPhotoCheckResultCallback,
27  onRecentPhotoClick: RecentPhotoClickCallback
28})
29
30Allows the application  to access the latest image or video in the user directory without the media access permission.
31
32**Decorator**: @Component
33
34**Atomic service API**: This API can be used in atomic services since API version 12.
35
36**System capability**: SystemCapability.FileManagement.PhotoAccessHelper.Core
37
38**Parameters**
39
40| Name         | Type           | Mandatory | Decorator Description     | Description                |
41|---------------|-----------------|-------|-----------------|--------------------------|
42| recentPhotoOptions         | [RecentPhotoOptions](#recentphotooptions)                             | No  | - | Configuration of the latest image or video. |
43| onRecentPhotoCheckResult   | [RecentPhotoCheckResultCallback](#recentphotocheckresultcallback)     | No  | - | Callback used to return the query result of the latest image or video. |
44| onRecentPhotoClick         | [RecentPhotoClickCallback](#recentphotoclickcallback)                 | Yes  | - | Callback to be called when the latest image or video is selected.     |
45
46## RecentPhotoOptions
47
48Represents the configuration of the latest image or video.
49
50**Atomic service API**: This API can be used in atomic services since API version 12.
51
52**System capability**: SystemCapability.FileManagement.PhotoAccessHelper.Core
53
54| Name                   | Type                                   | Mandatory | Description  |
55|-------------------------|-----------------------------------------|-------|--------|
56| period                  | number                                  | No   | Time period for the latest image or video, in seconds. The maximum value is **86400** seconds (one day), which is also the default value. If there is no image or video in the specified period, the component is not displayed. |
57| MIMEType                | [PhotoViewMIMETypes](js-apis-photoAccessHelper.md#photoviewmimetypes)   | No   | Types of the file displayed. The default value is **PhotoViewMIMETypes.IMAGE_VIDEO_TYPE**.                        |
58| photoSource             | [PhotoSource](#photosource)                                 | No   | Source of the latest image or video, for example, photo or video taken by the camera or screenshot. By default, the source is not restricted.                              |
59
60## RecentPhotoCheckResultCallback
61
62type RecentPhotoCheckResultCallback = (recentPhotoExists: boolean) => void
63
64Called to return the query result of the latest image or video.
65
66**Atomic service API**: This API can be used in atomic services since API version 12.
67
68**System capability**: SystemCapability.FileManagement.PhotoAccessHelper.Core
69
70**Parameters**
71
72| Name| Type| Mandatory| Description|
73| -------- | -------- | -------- | -------- |
74| recentPhotoExists | boolean | Yes| Whether the latest image or video exists.|
75
76## RecentPhotoClickCallback
77
78type RecentPhotoClickCallback = (recentPhotoInfo: BaseItemInfo) => boolean
79
80Called when the latest image or video is selected.
81
82**Atomic service API**: This API can be used in atomic services since API version 12.
83
84**System capability**: SystemCapability.FileManagement.PhotoAccessHelper.Core
85
86**Parameters**
87
88| Name| Type| Mandatory| Description|
89| -------- | -------- | -------- | -------- |
90| recentPhotoInfo | [BaseItemInfo](ohos-file-PhotoPickerComponent.md#baseiteminfo) | Yes| Information about the latest image or video.|
91
92**Return value**
93
94| Type   | Description                                                        |
95| ------- | ------------------------------------------------------------ |
96| boolean | Processing result of the latest image or video.|
97
98## PhotoSource
99
100Enumerates the sources of the image or video data.
101
102**Atomic service API**: This API can be used in atomic services since API version 12.
103
104**System capability**: SystemCapability.FileManagement.PhotoAccessHelper.Core
105
106| Name               | Value  | Description                                                                                                                |
107|-------------------|-----|--------------------------------------------------------------------------------------------------------------------|
108| ALL | 0   | Images and videos from all sources.|
109| CAMERA | 1   | Photo or video taken by the camera.|
110| SCREENSHOT | 2   | Screenshot or screen capture video.|
111
112## Example
113
114```ts
115// xxx.ets
116import {
117  photoAccessHelper
118} from '@kit.MediaLibraryKit';
119import {
120  RecentPhotoComponent, RecentPhotoOptions, PhotoSource, RecentPhotoCheckResultCallback, RecentPhotoClickCallback
121} from '@ohos.file.RecentPhotoComponent';
122import {
123  BaseItemInfo
124} from '@ohos.file.PhotoPickerComponent';
125
126@Entry
127@Component
128struct PickerDemo {
129  private recentPhotoOptions: RecentPhotoOptions = new RecentPhotoOptions();
130  private recentPhotoCheckResultCallback: RecentPhotoCheckResultCallback = (recentPhotoExists: boolean) => this.onRecentPhotoCheckResult(recentPhotoExists);
131  private recentPhotoClickCallback: RecentPhotoClickCallback = (recentPhotoInfo: BaseItemInfo): boolean => this.onRecentPhotoClick(recentPhotoInfo);
132
133  aboutToAppear() {
134    this.recentPhotoOptions.MIMEType = photoAccessHelper.PhotoViewMIMETypes.IMAGE_VIDEO_TYPE;
135    this.recentPhotoOptions.period = 30;
136    this.recentPhotoOptions.photoSource = PhotoSource.ALL;
137  }
138
139  private onRecentPhotoCheckResult(recentPhotoExists: boolean): void {
140    // Photo or video that meets the search criteria exists.
141    if (recentPhotoExists) {
142      console.info('The photo is exist.');
143    }
144  }
145
146  private onRecentPhotoClick(recentPhotoInfo: BaseItemInfo): boolean {
147    // Return the photo or video.
148    if (recentPhotoInfo) {
149      console.info('The photo uri is ' + recentPhotoInfo.uri);
150      return true;
151    }
152    return true;
153  }
154
155  build() {
156    Stack() {
157      RecentPhotoComponent({
158        recentPhotoOptions: this.recentPhotoOptions,
159        onRecentPhotoCheckResult: this.recentPhotoCheckResultCallback,
160        onRecentPhotoClick: this.recentPhotoClickCallback,
161      }).height('100%').width('100%')
162    }
163  }
164}
165```
166