HarmonyOS image.PixelMap保存到 相册问题

代码如下;

try {
  let imageUrl = await CpicImageUtil.savePixelMap(imgPix, FileUtil.getTempDirPath(), imgName)
  let uri = FileUtil.getFileUri(imageUrl).toString()
  //文件路径:file://com.cpic.cpicapp/data/storage/el2/base/haps/app/temp/SuperApp_1724046979648.png


  // 获取需要保存到媒体库的位于应用沙箱的图片/视频uri
  let srcFileUris: Array<string> = [
    uri
  ];

  let photoCreationConfigs: Array<photoAccessHelper.PhotoCreationConfig> = [
    {
      fileNameExtension: 'png',
      photoType: photoAccessHelper.PhotoType.IMAGE,
    }
  ];
  let desFileUris: Array<string> = await phAccessHelper.showAssetsCreationDialog(srcFileUris, photoCreationConfigs);
  console.info('showAssetsCreationDialog success, data is ' + desFileUris);
} catch (err) {
  console.error('showAssetsCreationDialog failed, errCode is ' + err.code + ', errMsg is ' + err.message);
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.

报错is not callable

HarmonyOS
2025-01-09 14:51:23
1184浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
Excelsior_abit

参考demo

import { photoAccessHelper } from '@kit.MediaLibraryKit';
import { image } from '@kit.ImageKit';
import { resourceManager } from '@kit.LocalizationKit';
import { BusinessError } from '@kit.BasicServicesKit';
import { fileIo, fileUri } from '@kit.CoreFileKit';
import { promptAction } from '@kit.ArkUI';

let context = getContext(this);
let phAccessHelper = photoAccessHelper.getPhotoAccessHelper(context);
@Entry
@Component
struct SaveImagePage {
  @State message: string = 'Hello World';
  @State pixelMap?: image.PixelMap = undefined
  async aboutToAppear(): Promise<void> {
    let decodingOptions: image.DecodingOptions = {
      editable: true,
      desiredPixelFormat: 3,
    }
    const rawFileDescriptor: resourceManager.RawFileDescriptor = await getContext(this).resourceManager.getRawFd('test.png');
    const imageSource: image.ImageSource = image.createImageSource(rawFileDescriptor);
    this.pixelMap = await imageSource.createPixelMap(decodingOptions)
  }
  build() {

    Column(){
      Image(this.pixelMap).width(200).height(200)
      Button('保存').onClick(async () =>{
        try {
          const imagePackerApi: image.ImagePacker = image.createImagePacker();
          let packOpts: image.PackingOption = { format: "image/jpeg", quality: 98 }
          let filePath = context.filesDir + "/" + "test.png"
          imagePackerApi.packing(this.pixelMap, packOpts, (err: BusinessError, data: ArrayBuffer) => {
            let file = fileIo.openSync(filePath,fileIo.OpenMode.CREATE | fileIo.OpenMode.READ_WRITE);
            fileIo.writeSync(file.fd,data)
          })
          let uri = fileUri.getUriFromPath(filePath);

          let srcFileUris: Array<string> = [
            uri // 实际场景请使用真实的uri
          ];
          let photoCreationConfigs: Array<photoAccessHelper.PhotoCreationConfig> = [
            {
              title: 'test', // 可选
              fileNameExtension: 'png',
              photoType: photoAccessHelper.PhotoType.IMAGE,
              subtype: photoAccessHelper.PhotoSubtype.DEFAULT, // 可选
            }
          ];

          let desFileUris: Array<string> = await phAccessHelper.showAssetsCreationDialog(srcFileUris, photoCreationConfigs);

          let imageFile = fileIo.openSync(desFileUris[0],fileIo.OpenMode.CREATE | fileIo.OpenMode.READ_WRITE);
          await fileIo.copyFile(filePath,imageFile.fd,0).then(() =>{
            promptAction.showToast({
              message:"下载成功,已保存到相册"
            })
          })
          await fileIo.close(imageFile.fd)

          console.info('showAssetsCreationDialog success, data is ' + desFileUris);
        } catch (err) {
          console.error('showAssetsCreationDialog failed, errCode is ' + err.code + ', errMsg is ' + err.message);
        }
      })
    }
  }
}
  • 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.
分享
微博
QQ
微信
回复
2025-01-09 17:45:01
相关问题
HarmonyOS如何将PixelMap保存到相册
1528浏览 • 1回复 待解决
HarmonyOS image.PixelMap的拉伸变形
659浏览 • 1回复 待解决
HarmonyOS 图片保存到相册
823浏览 • 1回复 待解决
HarmonyOS 图片保存到相册报错
1036浏览 • 1回复 待解决
HarmonyOS 保存到相册报错13900012
1292浏览 • 1回复 待解决
HarmonyOS 如何将图片保存到相册
773浏览 • 1回复 待解决
HarmonyOS 有没有保存到相册的组件
747浏览 • 1回复 待解决
HarmonyOS如何把图片保存到手机相册
1576浏览 • 1回复 待解决
获取网络图片并保存到相册
2843浏览 • 1回复 待解决
怎么把视频保存到相册以及主机端?
4951浏览 • 1回复 待解决