HarmonyOS 鸿蒙Next调起系统拍照后确认按钮无反应

发布于 1周前 作者 zlyuanteng 来自 鸿蒙OS

HarmonyOS 鸿蒙Next调起系统拍照后确认按钮无反应

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

3 回复

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

1.

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](/user/Entry)
[@Component](/user/Component)
struct Index {
  [@State](/user/State) path: string = '';
  [@State](/user/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: '拍摄失败' })
    }
  } 

2.

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)}`);
    }
  } 

 

针对HarmonyOS鸿蒙Next调起系统拍照后确认按钮无反应的问题,这通常是由于系统更新导致原有的API或action不再支持。在HarmonyOS中,如果wantConstant.Action.ACTION_IMAGE_CAPTURE这个action已废弃,那么你需要寻找新的方法来实现拍照功能。

首先,建议查阅最新的HarmonyOS官方文档或API参考,了解是否有新的API或action可以替代。其次,HarmonyOS可能提供了Camera Kit,这是一个专门用于处理相机功能的库,你可以检查是否可以通过Camera Kit来实现拍照功能。

此外,确保你的应用已经获得了访问相机和存储的权限,权限问题也可能导致拍照功能无法正常工作。

如果上述方法都无法解决问题,那么可能需要考虑使用其他方式来实现拍照功能,如使用第三方库或自定义相机界面。

如果问题依旧没法解决请联系官网客服,官网地址是:https://www.itying.com/category-93-b0.html

回到顶部