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 相机拍照模糊
346浏览 • 0回复 待解决
HarmonyOS 拉起相机拍照
234浏览 待解决
关于如何使用相机拍照模块拍照问题
1652浏览 • 0回复 待解决
HarmonyOS APP无法打开相机拍照
264浏览 • 1回复 待解决
HarmonyOS webviewJS 注入异常
31浏览 • 1回复 待解决
HarmonyOS 拍照返回uri转bitmap问题
333浏览 • 1回复 待解决
HarmonyOS拍照后调用openSync方法报错
375浏览 • 1回复 待解决
HarmonyOS json解析异常
312浏览 • 1回复 待解决
HarmonyOS 异常堆栈获取
268浏览 • 1回复 待解决
HarmonyOS 弹窗异常问题
221浏览 • 1回复 待解决
HarmonyOS 弹窗显示异常
125浏览 • 1回复 待解决
HarmonyOS bindSheet高度异常
188浏览 • 1回复 待解决
如何获取拍照图片地址
1835浏览 • 1回复 待解决
HarmonyOS Web 加载URL异常
60浏览 • 1回复 待解决
HarmonyOS onBackPress执行异常问题
381浏览 • 1回复 待解决
HarmonyOS Grid组件拖动异常
153浏览 • 1回复 待解决
HarmonyOS 自定义相机拍照后数据展示
510浏览 • 1回复 待解决
harmonyOS基于api9如何调用相机拍照
3620浏览 • 1回复 待解决