HarmonyOS 相册选择的文件路径如何转成ArrayBuffer

HarmonyOS
1天前
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
zbw_apple

可以参考以下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;

  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];
    1
    //读取图片为buffer
    const file = fs.openSync(this.albumPath, fs.OpenMode.READ_WRITE);
    this.photoSize = fs.statSync(file.fd).size;
    let buffer = new ArrayBuffer(this.photoSize);
    fs.readSync(file.fd, buffer);
    fs.closeSync(file);
    //解码成PixelMap
    const imageSource = image.createImageSource(buffer);
    this.pixel = await imageSource.createPixelMap({});
  }

  build() {
    Row() {
      Column() {
        Button(this.getAlbum)
          .onClick(() => {
            this.getPictureFromAlbum()
          })
        Image(this.pixel)
          .objectFit(ImageFit.None)
      }
      .width('100%')
    }
    .height('100%')
  }
}
分享
微博
QQ
微信
回复
1天前
相关问题
HarmonyOS pixelMap转成ArrayBuffer
89浏览 • 1回复 待解决
如何ArrayBuffer转成string?
629浏览 • 1回复 待解决
如何ArrayBuffer转成string
2582浏览 • 1回复 待解决
Uint8Array如何转成ArrayBuffer
1603浏览 • 1回复 待解决
在 string 转成ArrayBuffer报错
1035浏览 • 1回复 待解决
HarmonyOS怎么获取系统相册路径
450浏览 • 1回复 待解决
文件上传本地路径如何获取
448浏览 • 1回复 待解决
选择相册怎么等待返回啊?
2058浏览 • 1回复 待解决
拉起相册选择图片页面失败
1970浏览 • 1回复 待解决
HarmonyOS 关于从相册选择图片上传
37浏览 • 1回复 待解决
HarmonyOS自定义相册选择页面咨询
426浏览 • 1回复 待解决