HarmonyOS拍照saveUri的异常

配置了saveUri后,pickerResult.resultUri就是空的。getContext(this).cacheDir 是应用的沙盒地址,应该是有权限的,这样对吗?

let pickerProfile: picker.PickerProfile = { 
  cameraPosition: camera.CameraPosition.CAMERA_POSITION_BACK, 
  saveUri: getContext(this).cacheDir + "/haha.jpg" 
}; 
let pickerResult: picker.PickerResult = await picker.pick(getContext(this), 
  [picker.PickerMediaType.PHOTO], pickerProfile);
HarmonyOS
2024-08-12 15:09:27
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
FengTianYa

可以保存于沙箱路径,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 demo() { 
  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, picker.PickerMediaType.Video], 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}`); 
  } 
} 
@Entry 
@Component struct Photopage{ 
  build(){ 
    Column(){ 
      Button('拉起后置摄像头').onClick(()=>{ demo() }) 
    } 
  } 
}
分享
微博
QQ
微信
回复
2024-08-13 12:02:25
相关问题
HarmonyOS 拉起相机拍照
279浏览 待解决
HarmonyOS 相机拍照模糊
401浏览 • 0回复 待解决
HarmonyOS 相机-拍照之后预览
150浏览 • 1回复 待解决
HarmonyOS webviewJS 注入异常
96浏览 • 1回复 待解决
关于如何使用相机拍照模块拍照问题
1688浏览 • 0回复 待解决
HarmonyOS APP无法打开相机拍照
315浏览 • 1回复 待解决
HarmonyOS上如何控制相机拍照
57浏览 • 0回复 待解决
HarmonyOS json解析异常
353浏览 • 1回复 待解决
HarmonyOS 异常堆栈获取
312浏览 • 1回复 待解决
HarmonyOS 弹窗异常问题
264浏览 • 1回复 待解决
HarmonyOS 弹窗显示异常
164浏览 • 1回复 待解决
HarmonyOS bindSheet高度异常
250浏览 • 1回复 待解决
HarmonyOS拍照后调用openSync方法报错
432浏览 • 1回复 待解决
HarmonyOS 相机旋转横屏拍照问题
112浏览 • 1回复 待解决
HarmonyOS 拍照返回uri转bitmap问题
387浏览 • 1回复 待解决
HarmonyOS 数据库beginTrans使用异常
33浏览 • 1回复 待解决
HarmonyOS onBackPress执行异常问题
450浏览 • 1回复 待解决
HarmonyOS Web 加载URL异常
198浏览 • 1回复 待解决