HarmonyOS组件截图保存到相册失败,预览图灰色

代码如下:

import { componentSnapshot, componentUtils } from '@kit.ArkUI'; 
import { common } from '@kit.AbilityKit'; 
import { photoAccessHelper } from '@kit.MediaLibraryKit'; 
import fs from '@ohos.file.fs'; 
@Entry 
@Component 
struct PhotoPage { 
  @State pixelMap: PixelMap | undefined = undefined; 
  build() { 
    Column({ space: 20 }) { 
      Column() { 
        Text("Hello World") 
          .fontSize(18) 
          .fontColor(Color.White) 
      } 
      .width("80%") 
      .height(200) 
      .backgroundColor(Color.Blue) 
      .justifyContent(FlexAlign.Center) 
      .alignItems(HorizontalAlign.Center) 
      .borderRadius(16) 
      .id("_card_test") 
      SaveButton({ 
        icon: SaveIconStyle.FULL_FILLED, 
        text: SaveDescription.SAVE_IMAGE, buttonType: 
        ButtonType.Capsule 
      }) 
        .onClick(async (event: ClickEvent, result: SaveButtonOnClickResult) => { 
          if (result == SaveButtonOnClickResult.SUCCESS) { 
            try { 
              let pixelMap = await componentSnapshot.get("_card_test"); 
              let info = componentUtils.getRectangleById("_card_test"); 
              console.error("width: " + info.size.width + ", height: " + info.size.height); 
 
              this.pixelMap = pixelMap; 
              await this.savePhoto(pixelMap); 
            } catch (error) { 
              console.error("error is: " + JSON.stringify(error)); 
            } 
          } 
        }) 
      Image(this.pixelMap) 
        .width("80%") 
        .height(200) 
    } 
    .width("100%") 
    .height("100%") 
  } 
  async savePhoto(pixelMap: PixelMap): Promise<void> { 
    try { 
      // 将pixelMap写入到缓冲区ArrayBuffer中 
      let pixelBytesNumber: number = pixelMap.getPixelBytesNumber(); 
      const readBuffer: ArrayBuffer = new ArrayBuffer(pixelBytesNumber); 
      await pixelMap.readPixelsToBuffer(readBuffer); 
      console.error("readBuffer: " + readBuffer.byteLength); 
      // 创建空文件 
      let context = getContext(this) as common.UIAbilityContext; 
      let phAccessHelper = photoAccessHelper.getPhotoAccessHelper(context); 
      let uri = await phAccessHelper.createAsset(photoAccessHelper.PhotoType.IMAGE, 'jpg'); 
      console.error("uri: " + uri.toString()); 
      // 打开文件 
      let file = await fs.open(uri, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE); 
      // 将缓冲区数据写入到文件中 
      let writeLen = await fs.write(file.fd, readBuffer); 
      console.error("writeLen: " + writeLen); 
      // 关闭文件 
      await fs.close(file.fd); 
    } catch (error) { 
      console.error("savePhoto error: " + error); 
    } 
  } 
}
HarmonyOS
2024-08-22 20:17:35
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
zxjiu
// 将pixelMap写入到缓冲区ArrayBuffer中 
let pixelBytesNumber: number = pixelMap.getPixelBytesNumber(); 
const readBuffer: ArrayBuffer = new ArrayBuffer(pixelBytesNumber); 
await pixelMap.readPixelsToBuffer(readBuffer); 
console.error("readBuffer: " + readBuffer.byteLength);

存在问题,需要保存图片需要调用packing编码获取buffer,参考链接:https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/image-encoding-0000001861886509-V5​​https://docs.openharmony.cn/pages/v4.0/zh-cn/application-dev/media/image-overview.md

分享
微博
QQ
微信
回复
2024-08-23 14:07:43
相关问题
应用内组件截图保存到用户文件
1126浏览 • 1回复 待解决
HarmonyOS如何将PixelMap保存到相册
580浏览 • 1回复 待解决
HarmonyOS如何把图片保存到手机相册
518浏览 • 1回复 待解决
怎么把视频保存到相册以及主机端?
3862浏览 • 1回复 待解决
获取网络图片并保存到相册
1494浏览 • 1回复 待解决
如何把图片和文案结合,保存到相册
237浏览 • 0回复 待解决
有谁知道如何将图片保存到相册
1035浏览 • 1回复 待解决
如何将视频保存到相册以及主机端
5594浏览 • 1回复 待解决
HarmonyOS PhotoViewPicker 保存到图库
633浏览 • 1回复 待解决
HarmonyOS图片保存相册问题
443浏览 • 1回复 待解决