@ohos.file.photoAccessHelper (相册管理模块)
该模块提供相册管理模块能力,包括创建相册以及访问、修改相册中的媒体数据信息等。
import { photoAccessHelper } from '@kit.MediaLibraryKit';
import { image } from '@kit.ImageKit';
import { fileIo as fs } from '@kit.CoreFileKit';@Entry
@Component
struct Index {@State getAlbum: string = '显示相册中的图片';@State pixel: image.PixelMap | undefined = undefined;@State albumPath: string = '';@State photoSize: number = 0;async getPictureFromAlbum() {// 拉起相册,选择图片let PhotoSelectOptions = new photoAccessHelper.PhotoSelectOptions();PhotoSelectOptions.MIMEType = photoAccessHelper.PhotoViewMIMETypes.IMAGE_TYPE;PhotoSelectOptions.maxSelectNumber = 1;let photoPicker = new photoAccessHelper.PhotoViewPicker();let photoSelectResult: photoAccessHelper.PhotoSelectResult = await photoPicker.select(PhotoSelectOptions);this.albumPath = photoSelectResult.photoUris[0];// 读取图片为bufferconst file = fs.openSync(this.albumPath, fs.OpenMode.READ_ONLY);this.photoSize = fs.statSync(file.fd).size;console.info('Photo Size: ' + this.photoSize);let buffer = new ArrayBuffer(this.photoSize);fs.readSync(file.fd, buffer);fs.closeSync(file);// 解码成PixelMapconst imageSource = image.createImageSource(buffer);console.log('imageSource: ' + JSON.stringify(imageSource));this.pixel = await imageSource.createPixelMap({});}build() {Row() {Column() {Image(this.pixel).width('100%').aspectRatio(1)Button('显示照片').onClick(() => {this.getPictureFromAlbum();})}.width('100%')}.height('100%')}
}