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

HarmonyOS
2025-01-10 09:14:47
浏览
收藏 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%')
  }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
分享
微博
QQ
微信
回复
2025-01-10 10:11:07
相关问题
应用沙箱图片保存到图库
2294浏览 • 1回复 待解决
HarmonyOS如何图片保存到手机相册
1580浏览 • 1回复 待解决
如何图片和文案结合,保存到相册
1310浏览 • 0回复 待解决
HarmonyOS 如何base64图片保存到相册
1387浏览 • 1回复 待解决
HarmonyOS Resource文件如何保存到沙箱
929浏览 • 1回复 待解决
HarmonyOS 图片保存到相册
836浏览 • 1回复 待解决
如何将Pixmap保存到本地文件?
1223浏览 • 1回复 待解决
HarmonyOS 图片保存到相册报错
1038浏览 • 1回复 待解决