《鸿蒙开发-答案之书》使用系统相机拍照

74 阅读1分钟
《鸿蒙开发-答案之书》使用系统相机拍照

调用系统相机拍照,不用询问拍照权限

直接上代码:

/**
   * @desc : 使用系统相机拍照
   * @author : congge on 2024-07-13 10:56
   * 注:不用申请相机权限
   **/
  public static async  usePhoto(context: common.UIAbilityContext,callBack:(result:string)=>void) {
    try {
      let pickerProfile: picker.PickerProfile = { cameraPosition: camera.CameraPosition.CAMERA_POSITION_BACK }
      let pickerResult: picker.PickerResult = await picker.pick(context, [picker.PickerMediaType.PHOTO], pickerProfile)
      callBack(pickerResult.resultUri)
      console.log("the pick pickerResult is:" + JSON.stringify(pickerResult));
    } catch (error) {
      let err = error as BusinessError;
      console.error(`the pick call failed. error code: ${err.code}`);
    }
  }

有鸿蒙开发bug或者功能需求的可以私信我,我每天都看私信的