使用PhotoPicker组件访问图片/视频:当应用需要读取用户图片时,开发者可以在应用界面中嵌入PhotoPicker组件,在用户选择所需要的图片资源后,直接返回该图片资源,而不需要授予应用读取图片文件的权限,即可完成图片或视频文件的访问和读取。
this.pickerOptions.MIMEType = photoAccessHelper.PhotoViewMIMETypes.IMAGE_VIDEO_TYPE; this.pickerOptions.maxVideoSelectNumber = 9; this.pickerOptions.maxPhotoSelectNumber = 9; this.pickerOptions.maxSelectNumber = 9;
private onItemClicked(itemInfo: ItemInfo, clickType: ClickType): boolean { if (!itemInfo) { return false; } let mimeType: string | undefined = itemInfo.mimeType; let type: ItemType | undefined = itemInfo.itemType; let uri: string | undefined = itemInfo.uri; if (type === ItemType.CAMERA) { // 点击相机item。 return true; // 返回true则拉起系统相机,若应用需要自行处理则返回false。 } else { if (clickType === ClickType.SELECTED) { if (uri) { if (this.videoMimeType.some(videoType => videoType === mimeType)) { if (this.selectedMimeType && this.imageMimeType.some(imageType => imageType === this.selectedMimeType)) { try { this.getUIContext().getPromptAction().showToast({ message: `只能选择图片` }); } catch (error) { console.error(`failed to show toast: ${JSON.stringify(error)}`); } return false; } else { this.selectedUris.push(uri); this.selectedMimeType = mimeType; this.pickerOptions.preselectedUris = [...this.selectedUris]; return true; } } else if (this.imageMimeType.some(imageType => imageType === mimeType)) { if (this.selectedMimeType && this.videoMimeType.some(videoType => videoType === this.selectedMimeType)) { try { this.getUIContext().getPromptAction().showToast({ message: `只能选择视频` }); } catch (error) { console.error(`failed to show toast: ${JSON.stringify(error)}`); } return false; } else { this.selectedUris.push(uri); this.selectedMimeType = mimeType; this.pickerOptions.preselectedUris = [...this.selectedUris]; return true; } } else { try { this.getUIContext().getPromptAction().showToast({ message: `未知的图片或视频类型` }); } catch (error) { console.error(`failed to show toast: ${JSON.stringify(error)}`); } return false; } } } else { if (uri) { this.selectedUris = this.selectedUris.filter((item: string) => { return item != uri; }); if (this.selectedUris.length <= 0) { this.selectedMimeType = undefined; } this.pickerOptions.preselectedUris = [...this.selectedUris]; } } return true; } }
完整示例参考如下:
import { ClickType, ItemInfo, ItemType, PhotoPickerComponent, PickerController, PickerOptions, } from '@ohos.file.PhotoPickerComponent'; import photoAccessHelper from '@ohos.file.photoAccessHelper'; @Entry @Component struct PickerDemo { @State pickerController: PickerController = new PickerController(); pickerOptions: PickerOptions = new PickerOptions(); selectedMimeType: string | undefined = undefined; selectedUris: Array<string> = new Array<string>(); private videoMimeType: Array<string> = ['video/mp4']; // 视频类型的mimetype,根据需要设置 private imageMimeType: Array<string> = ['image/jpeg', 'image/png']; // 图片类型的mimetype,根据需要设置 aboutToAppear() { this.pickerOptions.MIMEType = photoAccessHelper.PhotoViewMIMETypes.IMAGE_VIDEO_TYPE; this.pickerOptions.maxVideoSelectNumber = 9; this.pickerOptions.maxPhotoSelectNumber = 9; this.pickerOptions.maxSelectNumber = 9; } private onItemClicked(itemInfo: ItemInfo, clickType: ClickType): boolean { if (!itemInfo) { return false; } let mimeType: string | undefined = itemInfo.mimeType; let type: ItemType | undefined = itemInfo.itemType; let uri: string | undefined = itemInfo.uri; if (type === ItemType.CAMERA) { // 点击相机item。 return true; // 返回true则拉起系统相机,若应用需要自行处理则返回false。 } else { if (clickType === ClickType.SELECTED) { if (uri) { if (this.videoMimeType.some(videoType => videoType === mimeType)) { if (this.selectedMimeType && this.imageMimeType.some(imageType => imageType === this.selectedMimeType)) { try { this.getUIContext().getPromptAction().showToast({ message: `只能选择图片` }); } catch (error) { console.error(`failed to show toast: ${JSON.stringify(error)}`); } return false; } else { this.selectedUris.push(uri); this.selectedMimeType = mimeType; this.pickerOptions.preselectedUris = [...this.selectedUris]; return true; } } else if (this.imageMimeType.some(imageType => imageType === mimeType)) { if (this.selectedMimeType && this.videoMimeType.some(videoType => videoType === this.selectedMimeType)) { try { this.getUIContext().getPromptAction().showToast({ message: `只能选择视频` }); } catch (error) { console.error(`failed to show toast: ${JSON.stringify(error)}`); } return false; } else { this.selectedUris.push(uri); this.selectedMimeType = mimeType; this.pickerOptions.preselectedUris = [...this.selectedUris]; return true; } } else { try { this.getUIContext().getPromptAction().showToast({ message: `未知的图片或视频类型` }); } catch (error) { console.error(`failed to show toast: ${JSON.stringify(error)}`); } return false; } } } else { if (uri) { this.selectedUris = this.selectedUris.filter((item: string) => { return item != uri; }); if (this.selectedUris.length <= 0) { this.selectedMimeType = undefined; } this.pickerOptions.preselectedUris = [...this.selectedUris]; } } return true; } } build() { Flex({ direction: FlexDirection.Column, justifyContent: FlexAlign.Center, alignItems: ItemAlign.Center }) { Column() { PhotoPickerComponent({ pickerOptions: this.pickerOptions, pickerController: this.pickerController, onItemClicked: (itemInfo: ItemInfo, clickType: ClickType): boolean => this.onItemClicked(itemInfo, clickType), }) .width('100%') .height('100%'); }; }; } }
Q:如何一次性获取全部图片与视频?
A:设置PickerOptions.MIMEType属性值为IMAGE_VIDEO_TYPE。
Q:PhotoPickerComponent组件是否支持全选中所有图片和视频,而不是手动选中?
A:PhotoPickerComponent组件当前不支持全选,另外PickerOptions规定maxPhotoSelectNumber和maxVideoSelectNumber的最大值和默认值为500。