文档管理中心
FAQS技术元服务截图报错问题定位

元服务截图报错问题定位

问题现象

在元服务中使用screenshot.pick方法截图报错,报错日志如下:

Failed to pick. Code: {"code":801}

背景知识

HarmonyOS开发中有三种截图方式,分别是组件截图snapshot窗口截图和屏幕截图

问题定位

在错误日志中发现关键信息Failed to pick. Code: {"code":801},在API参考通用错误码,801错误码含义为:该设备不支持此API。

分析结论

开发者在手机端使用screenshot.pick进行屏幕截图,该方法仅可在2in1设备上使用,因此手机端调用报错。

修改建议

应避免在手机设备上使用此API进行截图,推荐使用组件截图或窗口截图,如下示例代码使用窗口截图方法,支持在手机元服务中运行:

import { image } from '@kit.ImageKit';
import { window } from '@kit.ArkUI';
import { BusinessError } from '@ohos.base';

@Entry
@Component
struct Index {
  @State pixmap?: image.PixelMap = undefined;

  build() {
    Navigation() {
      Row() {
        Image(this.pixmap)
          .width('80%')
          .height('80%')
          .border({ color: Color.Black, width: 2 })
          .margin(5);
      };

      Row() {
        Button('窗口截图')
          .onClick(() => {
            let context: Context = this.getUIContext().getHostContext() as Context;
            window.getLastWindow(context, (err: BusinessError, data) => {
              const errCode: number = err.code;
              if (errCode) {
                console.error(`Failed to obtain the top window. Cause code: ${err.code}, message: ${err.message}`);
                return;
              }
              data.snapshot().then((pixelMap: image.PixelMap) => {
                this.pixmap = pixelMap;
              }).catch((err: BusinessError) => {
                console.error(`Failed to snapshot window. Cause code: ${err.code}, message: ${err.message}`);
              });
            });
          });
      };
    }
    .title('屏幕截图')
    .height('100%')
    .width('100%');
  }
}
在 FAQ 中进行搜索
请输入您想要搜索的关键词