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));
  }
}
HarmonyOS
3天前
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
FengTianYa

参考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%')
  }
}
分享
微博
QQ
微信
回复
3天前
相关问题
如何将一张图片转化为PixelMapElement
10049浏览 • 1回复 待解决
如何获取X509证书公钥,并转化成string
1014浏览 • 1回复 待解决
怎样才能把apk转化成鸿蒙应用?
6799浏览 • 1回复 待解决
有谁知道如何将图片保存到相册
1080浏览 • 1回复 待解决