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, RecentPhotoInfo, RecentPhotoCheckInfoCallback,
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  onRecentPhotoCheckInfo?: RecentPhotoCheckInfoCallback,
29})
30
31Allows the application  to access the latest image or video in the user directory without the media access permission.
32
33**Decorator**: @Component
34
35**System capability**: SystemCapability.FileManagement.PhotoAccessHelper.Core
36
37**Parameters**
38
39| Name                      | Type                                                               | Mandatory| Description                      |
40|--------------------------|-------------------------------------------------------------------|------|----------------------------|
41| recentPhotoOptions       | [RecentPhotoOptions](#recentphotooptions)                         | No | Configuration of the latest image or video.<br>**Atomic service API**: This API can be used in atomic services since API version 12.               |
42| onRecentPhotoCheckResult | [RecentPhotoCheckResultCallback](#recentphotocheckresultcallback) | No | Callback used to return the query result of the latest image or video.<br>**Atomic service API**: This API can be used in atomic services since API version 12.             |
43| onRecentPhotoClick       | [RecentPhotoClickCallback](#recentphotoclickcallback)             | Yes | Callback to be invoked when the latest image or video is selected.<br>**Atomic service API**: This API can be used in atomic services since API version 12.               |
44| onRecentPhotoCheckInfo<sup>13+</sup>   | [RecentPhotoCheckInfoCallback](#recentphotocheckinfocallback13)   | No | Callback used to return information about the latest image or video obtained.<br>**Atomic service API**: This API can be used in atomic services since API version 13. |
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.<br>If there is no image or video in the specified period, the component is not displayed.|
57| MIMEType                | [photoAccessHelper.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## RecentPhotoInfo<sup>13+</sup>
61
62Represents information about the latest image or video.
63
64**Atomic service API**: This API can be used in atomic services since API version 13.
65
66**System capability**: SystemCapability.FileManagement.PhotoAccessHelper.Core
67
68| Name        | Type    | Mandatory | Description                                                       |
69|------------|--------|-------|-----------------------------------------------------------|
70| dateTaken  | number | No   | Time 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).                    |
71| identifier | string | No   | Hash 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.|
72
73## RecentPhotoCheckResultCallback
74
75type RecentPhotoCheckResultCallback = (recentPhotoExists: boolean) => void
76
77Called to return the query result of the latest image or video.
78
79**Atomic service API**: This API can be used in atomic services since API version 12.
80
81**System capability**: SystemCapability.FileManagement.PhotoAccessHelper.Core
82
83**Parameters**
84
85| Name| Type| Mandatory| Description|
86| -------- | -------- | -------- | -------- |
87| recentPhotoExists | boolean | Yes| Whether the latest image or video exists.|
88
89## RecentPhotoClickCallback
90
91type RecentPhotoClickCallback = (recentPhotoInfo: BaseItemInfo) => boolean
92
93Called when the latest image or video is selected.
94
95**Atomic service API**: This API can be used in atomic services since API version 12.
96
97**System capability**: SystemCapability.FileManagement.PhotoAccessHelper.Core
98
99**Parameters**
100
101| Name| Type| Mandatory| Description|
102| -------- | -------- | -------- | -------- |
103| recentPhotoInfo | [BaseItemInfo](ohos-file-PhotoPickerComponent.md#baseiteminfo) | Yes| Information about the latest image or video.|
104
105**Return value**
106
107| Type   | Description                                                        |
108| ------- | ------------------------------------------------------------ |
109| boolean | Processing result of the latest image or video.|
110
111## RecentPhotoCheckInfoCallback<sup>13+</sup>
112
113type RecentPhotoCheckInfoCallback = (recentPhotoExists: boolean, info: RecentPhotoInfo) => void
114
115Called to return whether the latest image or video exists and the information about it.
116
117**Atomic service API**: This API can be used in atomic services since API version 13.
118
119**System capability**: SystemCapability.FileManagement.PhotoAccessHelper.Core
120
121**Parameters**
122
123| Name              | Type                                   | Mandatory| Description         |
124|-------------------|---------------------------------------| -------- |-------------|
125| recentPhotoExists | boolean                               | Yes| Whether the latest image or video exists.|
126| info              | [RecentPhotoInfo](#recentphotoinfo13) | Yes| Information about the latest image or video. |
127
128## PhotoSource
129
130Enumerates the sources of the image or video data.
131
132**Atomic service API**: This API can be used in atomic services since API version 12.
133
134**System capability**: SystemCapability.FileManagement.PhotoAccessHelper.Core
135
136| Name               | Value  | Description                                                                                                                |
137|-------------------|-----|--------------------------------------------------------------------------------------------------------------------|
138| ALL | 0   | Images and videos from all sources.|
139| CAMERA | 1   | Photo or video taken by the camera.|
140| SCREENSHOT | 2   | Screenshot or screen capture video.|
141
142## Example
143
144```ts
145// xxx.ets
146import {
147  photoAccessHelper
148} from '@kit.MediaLibraryKit';
149import {
150  RecentPhotoComponent, RecentPhotoOptions, PhotoSource, RecentPhotoInfo, RecentPhotoCheckResultCallback, RecentPhotoClickCallback, RecentPhotoCheckInfoCallback
151} from '@ohos.file.RecentPhotoComponent';
152import {
153  BaseItemInfo
154} from '@ohos.file.PhotoPickerComponent';
155
156@Entry
157@Component
158struct PickerDemo {
159  private recentPhotoOptions: RecentPhotoOptions = new RecentPhotoOptions();
160  private recentPhotoCheckResultCallback: RecentPhotoCheckResultCallback = (recentPhotoExists: boolean) => this.onRecentPhotoCheckResult(recentPhotoExists);
161  private recentPhotoClickCallback: RecentPhotoClickCallback = (recentPhotoInfo: BaseItemInfo): boolean => this.onRecentPhotoClick(recentPhotoInfo);
162  private recentPhotoCheckInfoCallback: RecentPhotoCheckInfoCallback = (recentPhotoExists: boolean, info: RecentPhotoInfo) => this.onRecentPhotoCheckInfo(recentPhotoExists, info);
163
164  aboutToAppear() {
165    this.recentPhotoOptions.MIMEType = photoAccessHelper.PhotoViewMIMETypes.IMAGE_VIDEO_TYPE;
166    this.recentPhotoOptions.period = 30;
167    this.recentPhotoOptions.photoSource = PhotoSource.ALL;
168  }
169
170  private onRecentPhotoCheckResult(recentPhotoExists: boolean): void {
171    // Photo or video that meets the search criteria exists.
172    if (recentPhotoExists) {
173      console.info('The photo is exist.');
174    }
175  }
176
177  private onRecentPhotoClick(recentPhotoInfo: BaseItemInfo): boolean {
178    // Return the photo or video.
179    if (recentPhotoInfo) {
180      console.info('The photo uri is ' + recentPhotoInfo.uri);
181      return true;
182    }
183    return true;
184  }
185
186  private onRecentPhotoCheckResult(recentPhotoExists: boolean, info: RecentPhotoInfo): void {
187    // Check whether a photo or video that meets the conditions exists. If yes, obtain information about the photo or video.
188  }
189
190  build() {
191    Stack() {
192      RecentPhotoComponent({
193        recentPhotoOptions: this.recentPhotoOptions,
194        onRecentPhotoCheckResult: this.recentPhotoCheckResultCallback,
195        onRecentPhotoClick: this.recentPhotoClickCallback,
196        onRecentPhotoCheckInfo: this.recentPhotoCheckInfoCallback,
197      }).height('100%').width('100%')
198    }
199  }
200}
201```
202