鸿蒙5.0&next开发【@ohos.file.RecentPhotoComponent (最近图片组件)】 媒体文件管理_鸿蒙recentphotocomponent刷新
应用可以在布局中嵌入最近图片组件,通过此组件,应用无需申请权限,即可指定配置访问公共目录中最近的一个图片或视频文件。授予的权限仅包含只读权限。
说明
该组件从API version 12开始支持。后续版本如有新增内容,则采用上角标单独标记该内容的起始版本。
导入模块
import { RecentPhotoComponent, RecentPhotoOptions, RecentPhotoCheckResultCallback, RecentPhotoClickCallback, PhotoSource, RecentPhotoInfo, RecentPhotoCheckInfoCallback,} from \'@ohos.file.RecentPhotoComponent\';
属性
支持[通用属性]
RecentPhotoComponent
RecentPhotoComponent({
recentPhotoOptions?: RecentPhotoOptions,
onRecentPhotoCheckResult?: RecentPhotoCheckResultCallback,
onRecentPhotoClick: RecentPhotoClickCallback,
onRecentPhotoCheckInfo?: RecentPhotoCheckInfoCallback,
})
RecentPhotoComponent,是最近图片组件,可用于访问公共目录下的图片/视频文件。通过此组件,应用无需申请媒体访问权限,即可根据配置项,访问公共目录下最新的一个图片或视频文件。
装饰器类型:@Component
系统能力:SystemCapability.FileManagement.PhotoAccessHelper.Core
参数:
RecentPhotoOptions
最近图片配置选项。
元服务API:从API version 12开始,该接口支持在元服务中使用。
系统能力:SystemCapability.FileManagement.PhotoAccessHelper.Core
RecentPhotoInfo13+
最近图片相关信息。
元服务API:从API version 13开始,该接口支持在元服务中使用。
系统能力:SystemCapability.FileManagement.PhotoAccessHelper.Core
RecentPhotoCheckResultCallback
type RecentPhotoCheckResultCallback = (recentPhotoExists: boolean) => void
最近图片查询结果回调事件。
元服务API: 从API version 12开始,该接口支持在元服务中使用。
系统能力:SystemCapability.FileManagement.PhotoAccessHelper.Core
参数:
RecentPhotoClickCallback
type RecentPhotoClickCallback = (recentPhotoInfo: BaseItemInfo) => boolean
选择最近图片触发的回调事件。
元服务API: 从API version 12开始,该接口支持在元服务中使用。
系统能力:SystemCapability.FileManagement.PhotoAccessHelper.Core
参数:
返回值:
RecentPhotoCheckInfoCallback13+
type RecentPhotoCheckInfoCallback = (recentPhotoExists: boolean, info: RecentPhotoInfo) => void
最近图片是否存在查询结果以及最近图片相关信息的回调事件。
元服务API: 从API version 13开始,该接口支持在元服务中使用。
系统能力:SystemCapability.FileManagement.PhotoAccessHelper.Core
参数:
PhotoSource
枚举,图片或者视频数据的来源类型。
元服务API:从API version 12开始,该接口支持在元服务中使用。
系统能力:SystemCapability.FileManagement.PhotoAccessHelper.Core
示例
// xxx.etsimport { photoAccessHelper} from \'@kit.MediaLibraryKit\';import { RecentPhotoComponent, RecentPhotoOptions, PhotoSource, RecentPhotoInfo, RecentPhotoCheckResultCallback, RecentPhotoClickCallback, RecentPhotoCheckInfoCallback} from \'@ohos.file.RecentPhotoComponent\';import { BaseItemInfo} from \'@ohos.file.PhotoPickerComponent\';@Entry@Componentstruct PickerDemo { private recentPhotoOptions: RecentPhotoOptions = new RecentPhotoOptions(); private recentPhotoCheckResultCallback: RecentPhotoCheckResultCallback = (recentPhotoExists: boolean) => this.onRecentPhotoCheckResult(recentPhotoExists); private recentPhotoClickCallback: RecentPhotoClickCallback = (recentPhotoInfo: BaseItemInfo): boolean => this.onRecentPhotoClick(recentPhotoInfo); private recentPhotoCheckInfoCallback: RecentPhotoCheckInfoCallback = (recentPhotoExists: boolean, info: RecentPhotoInfo) => this.onRecentPhotoCheckInfo(recentPhotoExists, info); aboutToAppear() { this.recentPhotoOptions.MIMEType = photoAccessHelper.PhotoViewMIMETypes.IMAGE_VIDEO_TYPE; this.recentPhotoOptions.period = 30; this.recentPhotoOptions.photoSource = PhotoSource.ALL; } private onRecentPhotoCheckResult(recentPhotoExists: boolean): void { // 存在符合条件的照片或视频。 if (recentPhotoExists) { console.info(\'The photo is exist.\'); } } private onRecentPhotoClick(recentPhotoInfo: BaseItemInfo): boolean { // 照片或视频返回。 if (recentPhotoInfo) { console.info(\'The photo uri is \' + recentPhotoInfo.uri); return true; } return true; } private onRecentPhotoCheckResult(recentPhotoExists: boolean, info: RecentPhotoInfo): void { // 是否存在符合条件的照片或视频,若存在则可以拿到该照片或视频的相关信息。 } build() { Stack() { RecentPhotoComponent({ recentPhotoOptions: this.recentPhotoOptions, onRecentPhotoCheckResult: this.recentPhotoCheckResultCallback, onRecentPhotoClick: this.recentPhotoClickCallback, onRecentPhotoCheckInfo: this.recentPhotoCheckInfoCallback, }).height(\'100%\').width(\'100%\') } }}