HarmonyOS 如何把photoAccessHelper拿到的图片uri保存到本地沙箱

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

参考demo:

import { photoAccessHelper } from '@kit.MediaLibraryKit';
import { BusinessError } from '@kit.BasicServicesKit';
import { image } from '@kit.ImageKit';
import fs from '@ohos.file.fs';
import { common } from '@kit.AbilityKit';
import fileIo from '@ohos.file.fs';

@Entry
@Component
struct SavePixelMapToAlbum {
  @State saveButtonOptions: SaveButtonOptions = {
    icon: SaveIconStyle.FULL_FILLED,
    text: SaveDescription.SAVE,
    buttonType: ButtonType.Capsule
  };

  async packToFile(pixelMap?: PixelMap): Promise<string> {
    // 获取应用文件路径
    let context = getContext(this) as common.UIAbilityContext;
    let filesDir: string = context.cacheDir;
    let picName = '/testimage' + new Date().getTime() + '.jpg'
    // 新建并打开文件
    let file = fs.openSync(filesDir + picName, fs.OpenMode.CREATE | fs.OpenMode.READ_WRITE);
    // let file = fileIo.openSync(filesDir + picName, fileIo.OpenMode.CREATE | fileIo.OpenMode.READ_WRITE);
    // 创建图像编码ImagePacker对象
    const imagePackerApi = image.createImagePacker();
    // // 设置编码输出流和编码参数。format为图像的编码格式;quality为图像质量,范围从0-100,100为最佳质量
    const options: image.PackingOption = { format: "image/jpeg", quality: 98 };
    await imagePackerApi.packToFile(pixelMap, file.fd, options)
    console.log(file.path);
    return file.path;
  }

  build() {
    Row() {
      Column() {
        Text('选择相册图片,并保存到沙盒路径')
        SaveButton(this.saveButtonOptions)
          .onClick(async (event, result: SaveButtonOnClickResult) => {
            if (result === SaveButtonOnClickResult.SUCCESS) {
              try {
                let PhotoSelectOptions = new photoAccessHelper.PhotoSelectOptions();
                PhotoSelectOptions.MIMEType = photoAccessHelper.PhotoViewMIMETypes.IMAGE_TYPE;
                PhotoSelectOptions.maxSelectNumber = 5;
                let photoPicker = new photoAccessHelper.PhotoViewPicker();
                photoPicker.select(PhotoSelectOptions)
                  .then(async (PhotoSelectResult: photoAccessHelper.PhotoSelectResult) => {
                    console.info('PhotoViewPicker.select successfully, PhotoSelectResult uri: ' +
                    JSON.stringify(PhotoSelectResult));
                    //获取相册图片URI
                    let filePath = PhotoSelectResult.photoUris[0]
                    let file = fileIo.openSync(filePath, fileIo.OpenMode.READ_ONLY)
                    //通过传入文件描述符来创建图片源实例
                    let imageSource: image.ImageSource = image.createImageSource(file.fd);
                    let pixelMap: image.PixelMap = await imageSource.createPixelMap();
                    //保存到沙盒路径
                    this.packToFile(pixelMap)
                  })
                  .catch((err: BusinessError) => {
                    console.error('PhotoViewPicker.select failed with err: ' + JSON.stringify(err));
                  });
              } catch (error) {
                let err: BusinessError = error as BusinessError;
                console.error('PhotoViewPicker failed with err: ' + JSON.stringify(err));
              }
            }
          })
      }
      .width('100%')
    }
    .height('100%')
  }
}
分享
微博
QQ
微信
回复
1天前
相关问题
应用沙箱图片保存到图库
1426浏览 • 1回复 待解决
HarmonyOS如何图片保存到手机相册
784浏览 • 1回复 待解决
如何图片和文案结合,保存到相册
486浏览 • 0回复 待解决
HarmonyOS Resource文件如何保存到沙箱
190浏览 • 1回复 待解决
HarmonyOS 图片保存到相册
18浏览 • 1回复 待解决
如何将Pixmap保存到本地文件?
628浏览 • 1回复 待解决
HarmonyOS 图片保存到相册报错
292浏览 • 1回复 待解决