HarmonyOS 拍照相关问题

// 拍照
async takePhoto() {
  let cacheDir = getContext(this).cacheDir
  try {
    let pickerProfile: picker.PickerProfile = {
      cameraPosition: camera.CameraPosition.CAMERA_POSITION_BACK,
      saveUri: cacheDir + '/' +RandomUtil.randomInt() + '.jpg'
    };
    let pickerResult: picker.PickerResult = await picker.pick(mContext,
      [picker.PickerMediaType.PHOTO], pickerProfile);
    if(pickerResult['resultCode'] == 0) {
      this.photoUri = pickerResult['resultUri']
    }
  } catch (error) {
    let err = error as BusinessError;
    console.error(`the pick call failed. error code: ${err.code}`);
  }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.

结果拿到的resultUri是空的, 请问是saveUri是没有权限吗?

HarmonyOS
2024-12-25 15:36:27
1614浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
Heiang

请参考下面demo来修改:

import picker from '@ohos.multimedia.cameraPicker'
import camera from '@ohos.multimedia.camera';
import common from '@ohos.app.ability.common';
import { BusinessError } from '@ohos.base';
import fileuri from '@ohos.file.fileuri';
import fs from '@ohos.file.fs';
let mContext = getContext(this) as common.Context;
class CameraPosition {
  cameraPosition : camera.CameraPosition
  saveUri :string
  constructor(cameraPosition : camera.CameraPosition,saveUri:string) {
    this.cameraPosition = cameraPosition
    this.saveUri = saveUri
  }
}
let pathDir = getContext().filesDir;
let filePath = pathDir + `/${new Date().getTime()}.jpg`
fs.createRandomAccessFileSync(filePath, fs.OpenMode.CREATE);
let uri = fileuri.getUriFromPath(filePath);
async function takePhoto() {
  try {
    let pickerProfile = new CameraPosition(camera.CameraPosition.CAMERA_POSITION_BACK,uri)
    //前置摄像机传CAMERA_POSITION_FRONT,后置摄像机传CAMERA_POSITION_BACK,saveuri传想存到对应沙箱的uri
    let pickerResult: picker.PickerResult = await picker.pick(mContext, [picker.PickerMediaType.PHOTO], pickerProfile);
    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}`);
  }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
分享
微博
QQ
微信
回复
2024-12-25 18:15:33


相关问题
HarmonyOS 调用相机拍照问题
794浏览 • 1回复 待解决
关于如何使用相机拍照模块拍照问题
2553浏览 • 0回复 待解决
HarmonyOS 相机拍照镜头问题
616浏览 • 1回复 待解决
HarmonyOS 相机旋转横屏拍照问题
1066浏览 • 1回复 待解决
HarmonyOS BindSheet相关问题
1404浏览 • 1回复 待解决
HarmonyOS 证书相关问题
1019浏览 • 1回复 待解决
HarmonyOS Grid相关问题
1240浏览 • 1回复 待解决
HarmonyOS KVStore 相关问题
1005浏览 • 1回复 待解决
HarmonyOS Lib相关问题
658浏览 • 1回复 待解决
HarmonyOS string相关问题
905浏览 • 1回复 待解决
HarmonyOS Worker相关问题
885浏览 • 1回复 待解决
HarmonyOS 线程相关问题
1293浏览 • 1回复 待解决
HarmonyOS AccountKit相关问题
1217浏览 • 1回复 待解决
HarmonyOS 拍照返回uri转bitmap问题
1102浏览 • 1回复 待解决
HarmonyOS RN相关问题
1363浏览 • 1回复 待解决
HarmonyOS @Event相关问题
886浏览 • 1回复 待解决
HarmonyOS CardRecognition相关问题
911浏览 • 1回复 待解决
HarmonyOS 混淆相关问题
771浏览 • 1回复 待解决
适配HarmonyOS相关问题
964浏览 • 1回复 待解决
HarmonyOS 地图相关问题
1544浏览 • 1回复 待解决
HarmonyOS RelativeContainer相关问题
720浏览 • 1回复 待解决
HarmonyOS protobuf相关问题
1261浏览 • 1回复 待解决
HarmonyOS音频相关问题
1507浏览 • 1回复 待解决