读取相册中的图片可参考如下demo:import photoAccessHelper from '@ohos.file.photoAccessHelper'; import image from '@ohos.multimedia.image'; import fs from '@ohos.file.fs'; @Entry @Component struct Index { @State getAlbum: string = '显示相册中的图片'; @State pixel: image.PixelMap | undefined = undefined; @State albumPath: string = ''; @State photoSize: number = 0; // @State img:ImageSource = $r('app.media.app_icon') 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]; // 读取图片为buffer const file = fs.openSync(this.albumPath, fs.OpenMode.READ_WRITE); 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); // 解码成PixelMap const 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%') } }通过使用photoAccessHelper.PhotoSelectOptions接口,详细可参考上述链接使用Picker选择媒体库资源:https://developer.huawei.com/consumer/cn/doc/harmonyos-guides/photoaccesshelper-resource-guidelines-V5\#%E4%BD%BF%E7%94%A8%E5%AE%89%E5%85%A8%E6%8E%A7%E4%BB%B6%E5%88%9B%E5%BB%BA%E5%AA%92%E4%BD%93%E8%B5%84%E6%BA%90https://developer.huawei.com/consumer/cn/doc/harmonyos-guides/photoaccesshelper-systemalbum-guidelines-V5
读取相册中的图片可参考如下demo:
通过使用photoAccessHelper.PhotoSelectOptions接口,详细可参考上述链接使用Picker选择媒体库资源:
https://developer.huawei.com/consumer/cn/doc/harmonyos-guides/photoaccesshelper-resource-guidelines-V5\#%E4%BD%BF%E7%94%A8%E5%AE%89%E5%85%A8%E6%8E%A7%E4%BB%B6%E5%88%9B%E5%BB%BA%E5%AA%92%E4%BD%93%E8%B5%84%E6%BA%90
https://developer.huawei.com/consumer/cn/doc/harmonyos-guides/photoaccesshelper-systemalbum-guidelines-V5