1# @ohos.file.RecentPhotoComponent (最近图片组件) 2<!--Kit: Media Library Kit--> 3<!--Subsystem: Multimedia--> 4<!--Owner: @xuchangda--> 5<!--Designer: @guxinggang--> 6<!--Tester: @wangbeibei--> 7<!--Adviser: @zengyawen--> 8 9应用可以在布局中嵌入最近图片组件,通过此组件,应用无需申请权限,即可指定配置访问公共目录中最近的一个图片或视频文件。授予的权限仅包含只读权限。 10 11> **说明:** 12> 13> 该组件从API version 12开始支持。后续版本如有新增内容,则采用上角标单独标记该内容的起始版本。 14 15## 导入模块 16 17```ts 18import { 19 RecentPhotoComponent, RecentPhotoOptions, RecentPhotoCheckResultCallback, 20 RecentPhotoClickCallback, PhotoSource, RecentPhotoInfo, RecentPhotoCheckInfoCallback, 21} from '@ohos.file.RecentPhotoComponent'; 22``` 23 24## 属性 25 26支持[通用属性](../apis-arkui/arkui-ts/ts-component-general-attributes.md)。 27 28## RecentPhotoComponent 29 30RecentPhotoComponent({ 31 recentPhotoOptions?: RecentPhotoOptions, 32 onRecentPhotoCheckResult?: RecentPhotoCheckResultCallback, 33 onRecentPhotoClick: RecentPhotoClickCallback, 34 onRecentPhotoCheckInfo?: RecentPhotoCheckInfoCallback, 35}) 36 37RecentPhotoComponent,是最近图片组件,可用于访问公共目录下的图片/视频文件。通过此组件,应用无需申请媒体访问权限,即可根据配置项,访问公共目录下最新的一个图片或视频文件。 38 39**装饰器类型**:@Component 40 41**系统能力**:SystemCapability.FileManagement.PhotoAccessHelper.Core 42 43**参数:** 44 45| 名称 | 类型 | 必填 | 说明 | 46|--------------------------|-------------------------------------------------------------------|------|----------------------------| 47| recentPhotoOptions | [RecentPhotoOptions](#recentphotooptions) | 否 | 最近图片配置参数信息。<br> **原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。 | 48| onRecentPhotoCheckResult | [RecentPhotoCheckResultCallback](#recentphotocheckresultcallback) | 否 | 最近图片查询结果回调函数。<br> **原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。 | 49| onRecentPhotoClick | [RecentPhotoClickCallback](#recentphotoclickcallback) | 是 | 选择最近图片回调函数。<br> **原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。 | 50| onRecentPhotoCheckInfo<sup>13+</sup> | [RecentPhotoCheckInfoCallback](#recentphotocheckinfocallback13) | 否 | 最近图片查询结果回调函数,并且返回该照片的相关信息。<br> **原子化服务API**:从API version 13开始,该接口支持在原子化服务中使用。 | 51 52## RecentPhotoOptions 53 54最近图片配置选项。 55 56**系统能力**:SystemCapability.FileManagement.PhotoAccessHelper.Core 57 58| 名称 | 类型 | 必填 | 说明 | 59|-------------------------|-----------------------------------------------------------------------------------------|-------|--------| 60| period | number | 否 | 配置显示多久时间段内的最近图片,单位为秒(s)。最长可配置时长为1天(86400s)。<br/>当值小于等于0、大于86400或者未配置时,默认按最长时间段1天显示最近图片。当配置时间段内无符合的图片或视频时,组件不显示。<br> **原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。| 61| MIMEType | [photoAccessHelper.PhotoViewMIMETypes](arkts-apis-photoAccessHelper-e.md#photoviewmimetypes) | 否 | 最近图片控件显示的文件类型,默认为PhotoViewMIMETypes.IMAGE_VIDEO_TYPE。<br> **原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。 | 62| photoSource | [PhotoSource](#photosource) | 否 | 配置最近图片视频显示内容的来源,比如拍照、截屏等。默认不限制来源。<br> **原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。 | 63| isAutoRefreshSupported<sup>20+</sup> | boolean | 否 | 配置最近照片组件在符合要求的最近图片或视频发生变更(包括新增、删除、修改)时是否进行刷新。<br/>当组件原显示的最近图片或视频被删除,而无符合要求的图片或视频时,则显示占位符,组件不自动退出。<br/>默认为false,不支持自动刷新;配置为true时显示全量照片;period字段失效。<br> **原子化服务API**:从API version 20开始,该接口支持在原子化服务中使用。| 64| colorMode<sup>20+</sup> | [PickerColorMode](ohos-file-PhotoPickerComponent.md#pickercolormode) | 否 | 支持应用配置占位符的颜色模式。<br/>当isAutoRefreshSupported为true,且无符合要求的最近图片或视频时,显示占位符,字段生效。<br/>默认为跟随系统深浅色模式。<br> **原子化服务API**:从API version 20开始,该接口支持在原子化服务中使用。| 65 66## RecentPhotoInfo<sup>13+</sup> 67 68最近图片相关信息。 69 70**原子化服务API**:从API version 13开始,该接口支持在原子化服务中使用。 71 72**系统能力**:SystemCapability.FileManagement.PhotoAccessHelper.Core 73 74| 名称 | 类型 | 必填 | 说明 | 75|------------|--------|-------|-----------------------------------------------------------| 76| dateTaken | number | 否 | 最近图片/视频的拍摄时间,单位为毫秒。(距1970年一月一日的毫秒数值)。 | 77| identifier | string | 否 | 最近图片/视频的名称hash值,用于辅助应用区分最新图片组件将要显示的图片/视频与之前曾显示过的图片/视频是否为同一个。 | 78 79## RecentPhotoCheckResultCallback 80 81type RecentPhotoCheckResultCallback = (recentPhotoExists: boolean) => void 82 83最近图片查询结果回调事件。 84 85**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 86 87**系统能力**:SystemCapability.FileManagement.PhotoAccessHelper.Core 88 89**参数:** 90 91| 参数名 | 类型 | 必填 | 说明 | 92| -------- | -------- | -------- | -------- | 93| recentPhotoExists | boolean | 是 | 查询最近图片是否存在,true为存在,false为不存在,默认为true。 | 94 95## RecentPhotoClickCallback 96 97type RecentPhotoClickCallback = (recentPhotoInfo: BaseItemInfo) => boolean 98 99选择最近图片触发的回调事件。 100 101**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 102 103**系统能力**:SystemCapability.FileManagement.PhotoAccessHelper.Core 104 105**参数:** 106 107| 参数名 | 类型 | 必填 | 说明 | 108| -------- | -------- | -------- | -------- | 109| recentPhotoInfo | [BaseItemInfo](ohos-file-PhotoPickerComponent.md#baseiteminfo) | 是 | 最近图片信息。 | 110 111**返回值:** 112 113| 类型 | 说明 | 114| ------- | ------------------------------------------------------------ | 115| boolean | 应用回调中处理最近图片的结果返回。true表示处理完成。 | 116 117## RecentPhotoCheckInfoCallback<sup>13+</sup> 118 119type RecentPhotoCheckInfoCallback = (recentPhotoExists: boolean, info: RecentPhotoInfo) => void 120 121最近图片是否存在查询结果以及最近图片相关信息的回调事件。 122 123**原子化服务API:** 从API version 13开始,该接口支持在原子化服务中使用。 124 125**系统能力**:SystemCapability.FileManagement.PhotoAccessHelper.Core 126 127**参数:** 128 129| 参数名 | 类型 | 必填 | 说明 | 130|-------------------|---------------------------------------| -------- |-------------| 131| recentPhotoExists | boolean | 是 | 查询最近图片是否存在,true为存在,false为不存在,默认为true。 | 132| info | [RecentPhotoInfo](#recentphotoinfo13) | 是 | 最近图片相关信息。 | 133 134## PhotoSource 135 136枚举,图片或者视频数据的来源类型。 137 138**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。 139 140**系统能力**:SystemCapability.FileManagement.PhotoAccessHelper.Core 141 142| 名称 | 值 | 说明 | 143|-------------------|-----|--------------------------------------------------------------------------------------------------------------------| 144| ALL | 0 | 所有来源的图片、视频。 | 145| CAMERA | 1 | 仅相机拍摄的图片、视频。 | 146| SCREENSHOT | 2 | 截屏图片或者录屏视频。 | 147 148## 示例 149 150```ts 151// xxx.ets 152import { 153 photoAccessHelper 154} from '@kit.MediaLibraryKit'; 155import { 156 RecentPhotoComponent, RecentPhotoOptions, PhotoSource, RecentPhotoInfo, RecentPhotoCheckResultCallback, RecentPhotoClickCallback, RecentPhotoCheckInfoCallback 157} from '@ohos.file.RecentPhotoComponent'; 158import { 159 BaseItemInfo 160} from '@ohos.file.PhotoPickerComponent'; 161 162@Entry 163@Component 164struct PickerDemo { 165 private recentPhotoOptions: RecentPhotoOptions = new RecentPhotoOptions(); 166 private recentPhotoCheckResultCallback: RecentPhotoCheckResultCallback = (recentPhotoExists: boolean) => this.onRecentPhotoCheckResult(recentPhotoExists); 167 private recentPhotoClickCallback: RecentPhotoClickCallback = (recentPhotoInfo: BaseItemInfo): boolean => this.onRecentPhotoClick(recentPhotoInfo); 168 private recentPhotoCheckInfoCallback: RecentPhotoCheckInfoCallback = (recentPhotoExists: boolean, info: RecentPhotoInfo) => this.onRecentPhotoCheckInfo(recentPhotoExists, info); 169 170 aboutToAppear() { 171 this.recentPhotoOptions.MIMEType = photoAccessHelper.PhotoViewMIMETypes.IMAGE_VIDEO_TYPE; 172 this.recentPhotoOptions.period = 30; 173 this.recentPhotoOptions.photoSource = PhotoSource.ALL; 174 } 175 176 private onRecentPhotoCheckResult(recentPhotoExists: boolean): void { 177 // 存在符合条件的照片或视频。 178 if (recentPhotoExists) { 179 console.info('The photo is exist.'); 180 } 181 } 182 183 private onRecentPhotoClick(recentPhotoInfo: BaseItemInfo): boolean { 184 // 照片或视频返回。 185 if (recentPhotoInfo) { 186 console.info('The photo uri is ' + recentPhotoInfo.uri); 187 return true; 188 } 189 return true; 190 } 191 192 private onRecentPhotoCheckInfo(recentPhotoExists: boolean, info: RecentPhotoInfo): void { 193 // 是否存在符合条件的照片或视频,若存在则可以拿到该照片或视频的相关信息。 194 } 195 196 build() { 197 Stack() { 198 RecentPhotoComponent({ 199 recentPhotoOptions: this.recentPhotoOptions, 200 onRecentPhotoCheckResult: this.recentPhotoCheckResultCallback, 201 onRecentPhotoClick: this.recentPhotoClickCallback, 202 onRecentPhotoCheckInfo: this.recentPhotoCheckInfoCallback, 203 }).height('100%').width('100%') 204 } 205 } 206} 207``` 208