HarmonyOS 调起系统拍照,点击拍照后的确认按钮点击无反应

通过action : wantConstant.Action.ACTION_IMAGE_CAPTURE调起系统拍照,点击拍照后,确认的按钮点击无反应.文档中说这个action已经废弃了,但没有找到新的action。

HarmonyOS
2024-08-12 15:12:03
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
FengTianYa

您可通过以下两种方式调起系统相机:

import { common, Want } from '@kit.AbilityKit'; 
import { BusinessError } from '@kit.BasicServicesKit'; 
import { fileIo, fileUri } from '@kit.CoreFileKit'; 
import { promptAction } from '@kit.ArkUI'; 
import { camera, cameraPicker } from '@kit.CameraKit'; 
const TAG = '[SystemCameraDemo]'; 
@Entry 
@Component 
struct Index { 
  @State path: string = ''; 
  @State isVideo: boolean = false; 
  private context = getContext(this) as common.UIAbilityContext; 
  async cameraPicker(type: cameraPicker.PickerMediaType) { 
    try { 
      let pickerProfile: cameraPicker.PickerProfile = { 
        // 相机的位置 
        cameraPosition: camera.CameraPosition.CAMERA_POSITION_BACK, 
        //saveUri 保存配置信息的uri 
        //videoDuration 录制的最大时长 
      }; 
      cameraPicker.pick(getContext(this), [type], pickerProfile) 
        .then((result: cameraPicker.PickerResult) => { 
          if (result.resultCode != 0) { 
            // 处理业务逻辑错误 
            console.error(TAG, `cameraPicker.pick failed, result is ${JSON.stringify(result)}`); 
            promptAction.showToast({ message: '拍摄失败' }) 
            return; 
          } 
          //若saveUri为空,resultUri为公共媒体路径。若saveUri不为空且具备写权限,resultUri与saveUri相同。若saveUri不为空且不具备写权限,则无法获取到resultUri 
          let uri: string = result.resultUri; 
          // 执行正常业务 
          console.info(TAG, `Succeeded in cameraPicker.pick. Data is ${JSON.stringify(result)}, uri is ${uri}`); 
          if (uri) { 
            // 保存到本地 
            this.save2Local(uri); 
          } else { 
            promptAction.showToast({ message: '拍摄失败' }) 
          } 
        }) 
    } catch (error) { 
      let err = error as BusinessError; 
      console.error(TAG, `the pick call failed. error code: ${err.code}`); 
      promptAction.showToast({ message: '拍摄失败' }) 
    } 
  } 
  startAbility(action: string, bundleName: string) { 
    let want: Want = { 
      action: action, 
      parameters: { 
        // 拍照完成后返回的应用BundleName 
        callBundleName: bundleName, 
        supportMultiMode: false 
      } 
    }; 
    try { 
      this.context.startAbilityForResult(want, (err: BusinessError, result: common.AbilityResult) => { 
        if (err.code) { 
          // 处理业务逻辑错误 
          console.error(TAG, `startAbilityForResult failed, code is ${err.code}, message is ${err.message}`); 
          return; 
        } 
        let uri: string = result?.want?.parameters?.resourceUri as string; 
        // 执行正常业务 
        console.info(TAG, `Succeeded in starting ability for result. Data is ${JSON.stringify(result)}, uri is ${uri}`); 
        if (uri) { 
          // 保存到本地 
          this.save2Local(uri); 
        } else { 
          promptAction.showToast({ message: '拍摄失败' }) 
        } 
      }); 
    } catch (err) { 
      let error = err as BusinessError; 
      console.error(TAG, `startAbilityForResult failed, err is ${JSON.stringify(error)}`); 
      promptAction.showToast({ message: '拍摄失败' }) 
    } 
  } 
  save2Local(uri: string) { 
    try { 
      let file = fileIo.openSync(uri, fileIo.OpenMode.READ_ONLY); 
      let prefix = uri.substring(uri.lastIndexOf('.') + 1); 
      let tempFileName = getContext(this).filesDir + '/' + new Date().getTime() + '.' + prefix; 
      let tempFile = fileIo.openSync(tempFileName, fileIo.OpenMode.READ_WRITE | fileIo.OpenMode.CREATE); 
      fileIo.copyFileSync(file.fd, tempFile.fd); 
      this.path = fileUri.getUriFromPath(tempFileName); 
      this.isVideo = (prefix == 'mp4' || prefix == 'MP4'); 
      console.info(TAG, `resolve2Sandbox successful.`); 
      promptAction.showToast({ message: '拍摄成功' }) 
    } catch (err) { 
      let error = err as BusinessError; 
      console.error(TAG, `resolve2Sandbox failed, err is ${JSON.stringify(error)}`); 
    } 
  }
分享
微博
QQ
微信
回复
2024-08-13 12:03:02
相关问题
HarmonyOS 点击图片按钮中间没反应
519浏览 • 1回复 待解决
点击拒接按钮无法再次来电
1968浏览 • 1回复 待解决
点击FindComponentById之后没有任何反应
7567浏览 • 2回复 待解决
如何调用系统相机拍照
1579浏览 • 1回复 待解决
HarmonyOS拍照调用openSync方法报错
439浏览 • 1回复 待解决
HarmonyOS 自定义相机拍照数据展示
598浏览 • 1回复 待解决
如何调用系统相机,拍照、录视频?
275浏览 • 1回复 待解决
关于如何使用相机拍照模块拍照问题
1704浏览 • 0回复 待解决
如何调用系统拍照并获取图片
572浏览 • 1回复 待解决
如何获取拍照的图片地址
1891浏览 • 1回复 待解决
如何实现按钮点击效果?
346浏览 • 2回复 待解决
HarmonyOS 拉起相机拍照
299浏览 待解决
HarmonyOS 相机拍照模糊
425浏览 • 0回复 待解决
js相机组件拍照自动保存吗
4100浏览 • 1回复 待解决