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**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。 36 37**系统能力**:SystemCapability.FileManagement.PhotoAccessHelper.Core 38 39**参数:** 40 41| 名称 | 类型 | 必填 | 参数描述 | 42|--------------------------|-------------------------------------------------------------------|------|----------------------------| 43| recentPhotoOptions | [RecentPhotoOptions](#recentphotooptions) | 否 | 最近图片配置参数信息。 | 44| onRecentPhotoCheckResult | [RecentPhotoCheckResultCallback](#recentphotocheckresultcallback) | 否 | 最近图片查询结果回调函数。 | 45| onRecentPhotoClick | [RecentPhotoClickCallback](#recentphotoclickcallback) | 是 | 选择最近图片回调函数。 | 46| onRecentPhotoCheckInfo<sup>13+</sup> | [RecentPhotoCheckInfoCallback](#recentphotocheckinfocallback13) | 否 | 最近图片查询结果回调函数,并且返回该照片的相关信息。 | 47 48## RecentPhotoOptions 49 50最近图片配置选项。 51 52**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。 53 54**系统能力**:SystemCapability.FileManagement.PhotoAccessHelper.Core 55 56| 名称 | 类型 | 必填 | 说明 | 57|-------------------------|-----------------------------------------------------------------------------------------|-------|--------| 58| period | number | 否 | 配置显示多久时间段内的最近图片,单位为秒。最长可配置时长为1天(86400)。<br/>未配置最长时间段时,默认按最长时间段1天显示最近图片。当配置时间段内无符合的图片或视频时,组件不显示。| 59| MIMEType | [photoAccessHelper.PhotoViewMIMETypes](js-apis-photoAccessHelper.md#photoviewmimetypes) | 否 | 最近图片控件显示的文件类型,默认为PhotoViewMIMETypes.IMAGE_VIDEO_TYPE。 | 60| photoSource | [PhotoSource](#photosource) | 否 | 配置最近图片视频显示内容的来源,比如拍照、截屏等。默认不限制来源。 | 61 62## RecentPhotoInfo<sup>13+</sup> 63 64最近图片相关信息。 65 66**原子化服务API**:从API version 13开始,该接口支持在原子化服务中使用。 67 68**系统能力**:SystemCapability.FileManagement.PhotoAccessHelper.Core 69 70| 名称 | 类型 | 必填 | 说明 | 71|------------|--------|-------|-----------------------------------------------------------| 72| dateTaken | number | 否 | 最近图片/视频的拍摄时间,单位为毫秒。(距1970年一月一日的毫秒数值)。 | 73| identifier | string | 否 | 最近图片/视频的名称hash值,用于辅助应用区分最新图片组件将要显示的图片/视频与之前曾显示过的图片/视频是否为同一个。 | 74 75## RecentPhotoCheckResultCallback 76 77type RecentPhotoCheckResultCallback = (recentPhotoExists: boolean) => void 78 79最近图片查询结果回调事件。 80 81**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 82 83**系统能力**:SystemCapability.FileManagement.PhotoAccessHelper.Core 84 85**参数:** 86 87| 参数名 | 类型 | 必填 | 说明 | 88| -------- | -------- | -------- | -------- | 89| recentPhotoExists | boolean | 是 | 查询最近图片是否存在。 | 90 91## RecentPhotoClickCallback 92 93type RecentPhotoClickCallback = (recentPhotoInfo: BaseItemInfo) => boolean 94 95选择最近图片触发的回调事件。 96 97**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 98 99**系统能力**:SystemCapability.FileManagement.PhotoAccessHelper.Core 100 101**参数:** 102 103| 参数名 | 类型 | 必填 | 说明 | 104| -------- | -------- | -------- | -------- | 105| recentPhotoInfo | [BaseItemInfo](ohos-file-PhotoPickerComponent.md#baseiteminfo) | 是 | 最近图片信息。 | 106 107**返回值:** 108 109| 类型 | 说明 | 110| ------- | ------------------------------------------------------------ | 111| boolean | 应用回调中处理最近图片的结果返回。 | 112 113## RecentPhotoCheckInfoCallback<sup>13+</sup> 114 115type RecentPhotoCheckInfoCallback = (recentPhotoExists: boolean, info: RecentPhotoInfo) => void 116 117最近图片是否存在查询结果以及最近图片相关信息的回调事件。 118 119**原子化服务API:** 从API version 13开始,该接口支持在原子化服务中使用。 120 121**系统能力**:SystemCapability.FileManagement.PhotoAccessHelper.Core 122 123**参数:** 124 125| 参数名 | 类型 | 必填 | 说明 | 126|-------------------|---------------------------------------| -------- |-------------| 127| recentPhotoExists | boolean | 是 | 查询最近图片是否存在。 | 128| info | [RecentPhotoInfo](#recentphotoinfo13) | 是 | 最近图片相关信息。 | 129 130## PhotoSource 131 132枚举,图片或者视频数据的来源类型。 133 134**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。 135 136**系统能力**:SystemCapability.FileManagement.PhotoAccessHelper.Core 137 138| 名称 | 值 | 说明 | 139|-------------------|-----|--------------------------------------------------------------------------------------------------------------------| 140| ALL | 0 | 所有来源的图片、视频。 | 141| CAMERA | 1 | 仅相机拍摄的图片、视频。 | 142| SCREENSHOT | 2 | 截屏图片或者录屏视频。 | 143 144## 示例 145 146```ts 147// xxx.ets 148import { 149 photoAccessHelper 150} from '@kit.MediaLibraryKit'; 151import { 152 RecentPhotoComponent, RecentPhotoOptions, PhotoSource, RecentPhotoInfo, RecentPhotoCheckResultCallback, RecentPhotoClickCallback, RecentPhotoCheckInfoCallback 153} from '@ohos.file.RecentPhotoComponent'; 154import { 155 BaseItemInfo 156} from '@ohos.file.PhotoPickerComponent'; 157 158@Entry 159@Component 160struct PickerDemo { 161 private recentPhotoOptions: RecentPhotoOptions = new RecentPhotoOptions(); 162 private recentPhotoCheckResultCallback: RecentPhotoCheckResultCallback = (recentPhotoExists: boolean) => this.onRecentPhotoCheckResult(recentPhotoExists); 163 private recentPhotoClickCallback: RecentPhotoClickCallback = (recentPhotoInfo: BaseItemInfo): boolean => this.onRecentPhotoClick(recentPhotoInfo); 164 private recentPhotoCheckInfoCallback: RecentPhotoCheckInfoCallback = (recentPhotoExists: boolean, info: RecentPhotoInfo) => this.onRecentPhotoCheckInfo(recentPhotoExists, info); 165 166 aboutToAppear() { 167 this.recentPhotoOptions.MIMEType = photoAccessHelper.PhotoViewMIMETypes.IMAGE_VIDEO_TYPE; 168 this.recentPhotoOptions.period = 30; 169 this.recentPhotoOptions.photoSource = PhotoSource.ALL; 170 } 171 172 private onRecentPhotoCheckResult(recentPhotoExists: boolean): void { 173 // 存在符合条件的照片或视频 174 if (recentPhotoExists) { 175 console.info('The photo is exist.'); 176 } 177 } 178 179 private onRecentPhotoClick(recentPhotoInfo: BaseItemInfo): boolean { 180 // 照片或视频返回 181 if (recentPhotoInfo) { 182 console.info('The photo uri is ' + recentPhotoInfo.uri); 183 return true; 184 } 185 return true; 186 } 187 188 private onRecentPhotoCheckResult(recentPhotoExists: boolean, info: RecentPhotoInfo): void { 189 // 是否存在符合条件的照片或视频,若存在则可以拿到该照片或视频的相关信息 190 } 191 192 build() { 193 Stack() { 194 RecentPhotoComponent({ 195 recentPhotoOptions: this.recentPhotoOptions, 196 onRecentPhotoCheckResult: this.recentPhotoCheckResultCallback, 197 onRecentPhotoClick: this.recentPhotoClickCallback, 198 onRecentPhotoCheckInfo: this.recentPhotoCheckInfoCallback, 199 }).height('100%').width('100%') 200 } 201 } 202} 203``` 204