HarmonyOS 保存PixelMap到相册,显示保存成功,相册中的图片为空白图片,宽高都是0

HarmonyOS
2024-12-19 15:52:04
954浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
zxjiu

可以参考:

import photoAccessHelper from '@ohos.file.photoAccessHelper';
import fs from '@ohos.file.fs';
import common from '@ohos.app.ability.common';
import promptAction from '@ohos.promptAction';
import { image } from '@kit.ImageKit';

@Entry
@Component
struct Index {
  @State icon: image.PixelMap | undefined = undefined;

  build() {
    Row() {
      Column({ space: 10 }) {
        SaveButton().onClick(async (event: ClickEvent, result: SaveButtonOnClickResult) => {
          if (result === SaveButtonOnClickResult.SUCCESS) {
            const context: common.UIAbilityContext =
              getContext(this) as common.UIAbilityContext; // 免去权限申请和权限请求等环节,获得临时授权,保存对应图片
            this.savePhotoToGallery(context);
          } else {
            promptAction.showToast({ message: '设置权限失败!' })
          }
        })
      }.width('100%')
    }.height('100%').backgroundColor(0xF1F3F5)
  }

  async savePhotoToGallery(context: common.UIAbilityContext) {
    const imagePackerApi: image.ImagePacker = image.createImagePacker();
    const color: ArrayBuffer =
      new ArrayBuffer(96); // 96为需要创建的像素buffer大小,取值为:height * width *4
    let opts: image.InitializationOptions = { editable: true, pixelFormat: 3, size: { height: 4, width: 6 } }
    this.icon = await image.createPixelMap(color, opts);
    image.createPixelMap(color, opts).then((pixelMap: image.PixelMap) => {
      let packOpts: image.PackingOption = { format: "image/jpeg", quality: 98 }
      imagePackerApi.packing(pixelMap, packOpts).then(async (buffer: ArrayBuffer) => {
        console.info('Succeeded in packing the image.');
        let helper = photoAccessHelper.getPhotoAccessHelper(context)
        let uri = await helper.createAsset(photoAccessHelper.PhotoType.IMAGE,
          'png') // 使用uri打开文件,可以持续写入内容,写入过程不受时间限制
        let file = await fs.open(uri, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE)
        try {
          context.resourceManager.getMediaContent($r('app.media.startIcon').id, 0)
            .then(async value => {
              let media = value.buffer;
              // 写到媒体库文件中
              await fs.write(file.fd, media);
              await fs.close(file.fd);
              promptAction.showToast({ message: '已保存至相册!' });
            });
        }
        catch (err) {
          console.error("error is "+ JSON.stringify(err))
        }
      })
    })
  }
}
  • 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.
分享
微博
QQ
微信
回复
2024-12-19 18:36:33
相关问题
HarmonyOS 实现保存图片相册
1104浏览 • 1回复 待解决
HarmonyOS 截图保存图片相册
805浏览 • 1回复 待解决
HarmonyOS 保存图片本地相册
949浏览 • 1回复 待解决
HarmonyOS 保存图片相册问题
1229浏览 • 1回复 待解决
HarmonyOS 保存图片系统相册
955浏览 • 1回复 待解决
如何保存本地图片相册
2133浏览 • 1回复 待解决
HarmonyOS 保存图片相册权限问题
1073浏览 • 1回复 待解决
如何保存网络图片相册
1644浏览 • 1回复 待解决
HarmonyOS保存图片系统相册问题咨询
1470浏览 • 1回复 待解决
求大佬告知如何保存图片相册
2094浏览 • 1回复 待解决
HarmonyOS 图片保存相册
807浏览 • 1回复 待解决
HarmonyOS图片保存相册问题
1443浏览 • 1回复 待解决
如何保存一张PNG图片相册
2653浏览 • 1回复 待解决
怎么实现保存网络图片相册功能?
1577浏览 • 1回复 待解决
HarmonyOS 图片保存相册报错
1011浏览 • 1回复 待解决
HarmonyOS h5如何实现保存图片相册
1036浏览 • 1回复 待解决
HarmonyOS 相册保存图片设置title报错
1040浏览 • 1回复 待解决