HarmonyOS 从相册选取完图片后获取图片uir路径后,如何将uir路径转化成ArrayBuffer类型?

代码如下:

async getPhoto() {
  try {
    let photoSelectOptions = new picker.PhotoSelectOptions();
    photoSelectOptions.MIMEType = picker.PhotoViewMIMETypes.IMAGE_TYPE;
    photoSelectOptions.maxSelectNumber = 1;
    let photoPicker = new picker.PhotoViewPicker();
    photoPicker.select(photoSelectOptions, (err: BusinessError, photoSelectResult: picker.PhotoSelectResult) => {
      if (err) {
        console.error('PhotoViewPicker.select failed with err: ' + JSON.stringify(err));
        return;
      }
      console.info('PhotoViewPicker.select successfully, photoSelectResult uri: ' + JSON.stringify(photoSelectResult));

    });
  } catch (error) {
    let err: BusinessError = error as BusinessError;
    console.error('PhotoViewPicker failed with err: ' + JSON.stringify(err));
  }
}
阅读 408
1 个回答

参考demo如下

import { BusinessError } from '@kit.BasicServicesKit';
import { picker } from '@kit.CoreFileKit';
import fs from '@ohos.file.fs';
import buffer from '@ohos.buffer';
@Entry
@Component
struct Index {
  @State message: string = 'Hello World';

  async getPhoto() {
    try {
      let context = getContext()
      let photoSelectOptions = new picker.PhotoSelectOptions();
      photoSelectOptions.MIMEType = picker.PhotoViewMIMETypes.IMAGE_TYPE;
      photoSelectOptions.maxSelectNumber = 1;
      let photoPicker = new picker.PhotoViewPicker();
      photoPicker.select(photoSelectOptions, (err: BusinessError, photoSelectResult: picker.PhotoSelectResult) => {
        if (err) {
          console.error('PhotoViewPicker.select failed with err: ' + JSON.stringify(err));
          return;
        }
        let uris = photoSelectResult.photoUris.toString();
        let file = fs.openSync(uris, fs.OpenMode.READ_ONLY);
        let arrayBuffer = new ArrayBuffer(1024);
        let readLength = 0
        let readTotalLength = 0
        let fitstOffset = 0
        let newFile = fs.openSync( context.filesDir+ '/new.jpg', fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE)

        let fileLength = 12000
        while ((readLength = fs.readSync(file.fd, arrayBuffer,
          {
            offset: fitstOffset + readTotalLength,
            length: fileLength > arrayBuffer.byteLength ? 1024 : fileLength
          })) > 0) {
          readTotalLength += readLength
          fs.writeSync(newFile.fd, arrayBuffer, { length: readLength })
          fileLength -= arrayBuffer.byteLength

          let buf = buffer.from(arrayBuffer, 0, readLength);
          console.info(`The content of file: ${buf.toString()}`);
          console.log('--------------------------------------------');
        }
        fs.closeSync(file)
        fs.closeSync(newFile)

        // let readLen = fs.readSync(file.fd, arrayBuffer);
        // fs.writeSync(newFile.fd, arrayBuffer,  { length: readLen })
        // let buf = buffer.from(arrayBuffer, 0, readLen);
        // console.info(`The content of file: ${buf.toString()}`);
        // console.info('readSync data to file succeed and buffer size is:' + readLen);

      });
    } catch (error) {
      let err: BusinessError = error as BusinessError;
      console.error('PhotoViewPicker failed with err: ' + JSON.stringify(err));
    }
  }

  build() {
    RelativeContainer() {
      Text(this.message)
        .id('HelloWorld')
        .fontSize(50)
        .fontWeight(FontWeight.Bold)
        .alignRules({
          center: { anchor: '__container__', align: VerticalAlign.Center },
          middle: { anchor: '__container__', align: HorizontalAlign.Center }
        })
        .onClick(() => {
          this.getPhoto()
        })
    }
    .height('100%')
    .width('100%')
  }
}
logo
HarmonyOS
子站问答
访问
宣传栏