HarmonyOS鸿蒙Next中相册和拍照需要申请权限,是否有其他解决方案可供使用

HarmonyOS鸿蒙Next中相册和拍照需要申请权限,是否有其他解决方案可供使用

5 回复

操作相机(拍照和录像)

// 唤起相机
async openCamera() {
  const profile: cameraPicker.PickerProfile = {
    cameraPosition: camera.CameraPosition.CAMERA_POSITION_BACK
  }
  const result: cameraPicker.PickerResult = await cameraPicker.pick(getContext(),
    [cameraPicker.PickerMediaType.PHOTO, cameraPicker.PickerMediaType.VIDEO],
    profile)
  // promptAction.showDialog({
  //   message: JSON.stringify(result)
  // })
  // result.resultUri
  if (result.mediaType === 'photo' && result.resultUri !== '') {
    //   拍照
    const newFile = getContext().filesDir + '/' + Date.now() + '.jpg'
    const file = fileIo.openSync(result.resultUri, fileIo.OpenMode.READ_ONLY)
    fileIo.copyFileSync(file.fd, newFile)
    const newMessage = new MessageInfoModel({
      sendUser: currentUser,
      connectUser: this.talker,
      messageContent: '[图片]',
      messageType: MessageTypeEnum.IMAGE,
      sourceFilePath: newFile
    } as MessageInfo)
    // 借用发图的方法
    this.sendImageMsg([newMessage])
  } else if (result.mediaType === 'video' && result.resultUri !== '') {
    //   拍视频
    const newFile = getContext().filesDir + '/' + Date.now() + '.mp4'
    const file = fileIo.openSync(result.resultUri, fileIo.OpenMode.READ_ONLY)
    fileIo.copyFileSync(file.fd, newFile)
    const newMessage = new MessageInfoModel({
      sendUser: currentUser,
      connectUser: this.talker,
      messageContent: '[视频]',
      messageType: MessageTypeEnum.VIDEO,
      sourceFilePath: newFile,
    } as MessageInfo)
    this.sendImageMsg([newMessage])
  }
}

拍照和从相机选则照片

//拍照
async takePhoto() {
  let myContext = getContext() as common.Context
  const result = await cameraPicker.pick(myContext,
    [cameraPicker.PickerMediaType.PHOTO],
    {
      cameraPosition: camera.CameraPosition.CAMERA_POSITION_BACK
    }
  )
  Log.info('takePhoto:' + result.resultUri)
  // 都是拿到了图
  // result.resultUri
  if (result.resultUri) {
    this.callback(result.resultUri)
  }
}
//从相册选则
async pickPhoto() {
  // 变动:从picker 刚更新为 photoAccessHelper
  const photo = new photoAccessHelper.PhotoViewPicker()
  const result = await photo.select({
    MIMEType: photoAccessHelper.PhotoViewMIMETypes.IMAGE_TYPE,
    maxSelectNumber: 1
  })
  // 都是拿到了图
  // result
  Log.info('picker' + result.photoUris[0])
  if (result.photoUris?.length) {
    this.callback(result.photoUris[0])
  }
}

saveButton写入相册

SaveButton-安全控件-ArkTS组件-ArkUI(方舟UI框架)-应用框架 - 华为HarmonyOS开发者 (huawei.com)

更多关于HarmonyOS鸿蒙Next中相册和拍照需要申请权限,是否有其他解决方案可供使用的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


若需避免直接申请相机或相册权限,可使用系统选择器(Picker)

1/图片选择场景:通过 PhotoViewPicker 调用系统相册界面,用户选择图片时自动获取临时权限,无需主动申请 ohos.permission.READ_IMAGEVIDEO。

示例:

let photoPicker = new picker.PhotoViewPicker();

photoPicker.select().then((photoSelectResult) => {

  // 处理用户选择的图片

});

2/拍照或录像场景:使用 CameraPicker 直接调用系统相机界面,用户操作时临时授权,无需应用主动申请 ohos.permission.CAMERA。适用基础拍摄需求,如上传头像、拍摄简单内容。

CameraPicker(系统相机选择器)功能:拉起系统相机界面进行拍照或录像,PhotoViewPicker(相册选择器)功能:拉起系统相册界面

参考链接:https://developer.huawei.com/consumer/cn/doc/best-practices/bpta-image_get_and_save

https://developer.huawei.com/consumer/cn/doc/harmonyos-guides/camera-picker

在HarmonyOS Next中,相册和拍照功能必须申请权限。目前系统权限管理严格,无其他替代方案。权限申请需通过系统API完成,确保用户隐私和数据安全。

在HarmonyOS Next中,相册和拍照功能确实需要申请权限,这是系统安全机制的一部分。目前没有绕过权限申请的替代方案,开发者必须遵循权限申请流程,通过弹窗提示用户授权后才能访问相关功能。建议在应用设计时优化权限申请时机和提示文案,提升用户体验。

回到顶部