1# @ohos.file.RecentPhotoComponent (最近图片组件)
2
3应用可以在布局中嵌入最近图片组件,通过此组件,应用无需申请权限,即可指定配置访问公共目录中最近的一个图片或视频文件。授予的权限仅包含只读权限。
4
5> **说明:**
6>
7> 该组件从API version 12开始支持。后续版本如有新增内容,则采用上角标单独标记该内容的起始版本。
8
9## 导入模块
10
11```ts
12import {
13  RecentPhotoComponent, RecentPhotoOptions, RecentPhotoCheckResultCallback,
14  RecentPhotoClickCallback, PhotoSource, RecentPhotoInfo, RecentPhotoCheckInfoCallback,
15} from '@ohos.file.RecentPhotoComponent';
16```
17
18## 属性
19
20支持[通用属性](../apis-arkui/arkui-ts/ts-universal-attributes-size.md)。
21
22## RecentPhotoComponent
23
24RecentPhotoComponent({
25  recentPhotoOptions?: RecentPhotoOptions,
26  onRecentPhotoCheckResult?: RecentPhotoCheckResultCallback,
27  onRecentPhotoClick: RecentPhotoClickCallback,
28  onRecentPhotoCheckInfo?: RecentPhotoCheckInfoCallback,
29})
30
31RecentPhotoComponent,是最近图片组件,可用于访问公共目录下的图片/视频文件。通过此组件,应用无需申请媒体访问权限,即可根据配置项,访问公共目录下最新的一个图片或视频文件。
32
33**装饰器类型**:@Component
34
35**系统能力**:SystemCapability.FileManagement.PhotoAccessHelper.Core
36
37**参数:**
38
39| 名称                       | 类型                                                                | 必填 | 说明                       |
40|--------------------------|-------------------------------------------------------------------|------|----------------------------|
41| recentPhotoOptions       | [RecentPhotoOptions](#recentphotooptions)                         | 否  | 最近图片配置参数信息。<br> **原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。                |
42| onRecentPhotoCheckResult | [RecentPhotoCheckResultCallback](#recentphotocheckresultcallback) | 否  | 最近图片查询结果回调函数。<br> **原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。              |
43| onRecentPhotoClick       | [RecentPhotoClickCallback](#recentphotoclickcallback)             | 是  | 选择最近图片回调函数。<br> **原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。                |
44| onRecentPhotoCheckInfo<sup>13+</sup>   | [RecentPhotoCheckInfoCallback](#recentphotocheckinfocallback13)   | 否  | 最近图片查询结果回调函数,并且返回该照片的相关信息。<br> **原子化服务API**:从API version 13开始,该接口支持在原子化服务中使用。 |
45
46## RecentPhotoOptions
47
48最近图片配置选项。
49
50**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。
51
52**系统能力**:SystemCapability.FileManagement.PhotoAccessHelper.Core
53
54| 名称                    | 类型                                                                                      | 必填  | 说明   |
55|-------------------------|-----------------------------------------------------------------------------------------|-------|--------|
56| period                  | number                                                                                  | 否    | 配置显示多久时间段内的最近图片,单位为秒。最长可配置时长为1天(86400)。<br/>未配置最长时间段时,默认按最长时间段1天显示最近图片。当配置时间段内无符合的图片或视频时,组件不显示。|
57| MIMEType                | [photoAccessHelper.PhotoViewMIMETypes](js-apis-photoAccessHelper.md#photoviewmimetypes) | 否    | 最近图片控件显示的文件类型,默认为PhotoViewMIMETypes.IMAGE_VIDEO_TYPE。                         |
58| photoSource             | [PhotoSource](#photosource)                                                             | 否    | 配置最近图片视频显示内容的来源,比如拍照、截屏等。默认不限制来源。                               |
59
60## RecentPhotoInfo<sup>13+</sup>
61
62最近图片相关信息。
63
64**原子化服务API**:从API version 13开始,该接口支持在原子化服务中使用。
65
66**系统能力**:SystemCapability.FileManagement.PhotoAccessHelper.Core
67
68| 名称         | 类型     | 必填  | 说明                                                        |
69|------------|--------|-------|-----------------------------------------------------------|
70| dateTaken  | number | 否    | 最近图片/视频的拍摄时间,单位为毫秒。(距1970年一月一日的毫秒数值)。                     |
71| identifier | string | 否    | 最近图片/视频的名称hash值,用于辅助应用区分最新图片组件将要显示的图片/视频与之前曾显示过的图片/视频是否为同一个。 |
72
73## RecentPhotoCheckResultCallback
74
75type RecentPhotoCheckResultCallback = (recentPhotoExists: boolean) => void
76
77最近图片查询结果回调事件。
78
79**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。
80
81**系统能力**:SystemCapability.FileManagement.PhotoAccessHelper.Core
82
83**参数:**
84
85| 参数名 | 类型 | 必填 | 说明 |
86| -------- | -------- | -------- | -------- |
87| recentPhotoExists | boolean | 是 | 查询最近图片是否存在。 |
88
89## RecentPhotoClickCallback
90
91type RecentPhotoClickCallback = (recentPhotoInfo: BaseItemInfo) => boolean
92
93选择最近图片触发的回调事件。
94
95**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。
96
97**系统能力**:SystemCapability.FileManagement.PhotoAccessHelper.Core
98
99**参数:**
100
101| 参数名 | 类型 | 必填 | 说明 |
102| -------- | -------- | -------- | -------- |
103| recentPhotoInfo | [BaseItemInfo](ohos-file-PhotoPickerComponent.md#baseiteminfo) | 是 | 最近图片信息。 |
104
105**返回值:**
106
107| 类型    | 说明                                                         |
108| ------- | ------------------------------------------------------------ |
109| boolean | 应用回调中处理最近图片的结果返回。 |
110
111## RecentPhotoCheckInfoCallback<sup>13+</sup>
112
113type RecentPhotoCheckInfoCallback = (recentPhotoExists: boolean, info: RecentPhotoInfo) => void
114
115最近图片是否存在查询结果以及最近图片相关信息的回调事件。
116
117**原子化服务API:** 从API version 13开始,该接口支持在原子化服务中使用。
118
119**系统能力**:SystemCapability.FileManagement.PhotoAccessHelper.Core
120
121**参数:**
122
123| 参数名               | 类型                                    | 必填 | 说明          |
124|-------------------|---------------------------------------| -------- |-------------|
125| recentPhotoExists | boolean                               | 是 | 查询最近图片是否存在。 |
126| info              | [RecentPhotoInfo](#recentphotoinfo13) | 是 | 最近图片相关信息。   |
127
128## PhotoSource
129
130枚举,图片或者视频数据的来源类型。
131
132**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。
133
134**系统能力**:SystemCapability.FileManagement.PhotoAccessHelper.Core
135
136| 名称                | 值   | 说明                                                                                                                 |
137|-------------------|-----|--------------------------------------------------------------------------------------------------------------------|
138| ALL | 0   | 所有来源的图片、视频。 |
139| CAMERA | 1   | 仅相机拍摄的图片、视频。 |
140| SCREENSHOT | 2   | 截屏图片或者录屏视频。 |
141
142## 示例
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    // 存在符合条件的照片或视频
172    if (recentPhotoExists) {
173      console.info('The photo is exist.');
174    }
175  }
176
177  private onRecentPhotoClick(recentPhotoInfo: BaseItemInfo): boolean {
178    // 照片或视频返回
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    // 是否存在符合条件的照片或视频,若存在则可以拿到该照片或视频的相关信息
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