HarmonyOS拍照后调用openSync方法报错

拍照取到照片uri后,进行base64转码,第一步需要先调用fs.openSync(uri,fs.OpenMode.READ_ONLY)方法,但是调用时报错,提示Operation not permitted。

HarmonyOS
2024-09-09 10:47:01
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
FengTianYa

参考代码:

Button(this.message) 
  .fontSize(50) 
  .fontWeight(FontWeight.Bold) 
  .onClick(() => { 
    this.uri2PixelMap() 
  }) 
Image('data:image/jpeg;base64,' + this.pixel) 
  .width("100%") 
  .height("70%") 
  .border({ width: 1, color: Color.Red }) 
@State pixel: string | undefined = undefined; 
async uri2PixelMap() { 
  try { 
    let pickerProfile: picker.PickerProfile = { 
      cameraPosition: camera.CameraPosition.CAMERA_POSITION_BACK 
    }; 
    let pickerResult: picker.PickerResult = await picker.pick(getContext(this), 
      [picker.PickerMediaType.PHOTO], pickerProfile); 
    console.error("相册返回值=" + pickerResult.resultUri); 
    console.log("the pick pickerResult is:" + JSON.stringify(pickerResult.resultUri)); 
    const tempath = await this.beginScan(pickerResult.resultUri); 
    console.log(tempath) 
    const imageSource: image.ImageSource = image.createImageSource(tempath); 
    const pixelMap: image.PixelMap = await imageSource.createPixelMap(); 
    const imagePackerApi: image.ImagePacker = image.createImagePacker(); 
    let packOpts: image.PackingOption = { format: "image/jpeg", quality: 98 }; 
    imagePackerApi.packing(pixelMap, packOpts) 
      .then((data: ArrayBuffer) => { 
        console.info('packing succeeded.'); 
        let bufferArr = new Uint8Array(data) 
        let help = new util.Base64Helper 
        this.pixel = help.encodeToStringSync(bufferArr) 
        console.log("base64 => " + this.pixel) 
      }).catch((error: BusinessError) => { 
      console.error('packing failed.'); 
    }) 
  } catch (error) { 
    let err = error as BusinessError; 
    console.error(`the pick call failed. error code: ${err.code}`); 
  } 
} 
async beginScan(url: string) { 
  const tempPath = getContext().filesDir + "/" + util.generateRandomUUID() + ".jpg" 
  const file = fileIo.openSync(url, fileIo.OpenMode.READ_ONLY); 
  fileIo.copyFileSync(file.fd, tempPath); 
  return tempPath; 
}
分享
微博
QQ
微信
回复
2024-09-09 17:07:09
相关问题
调用监听折叠屏状态的方法报错
256浏览 • 1回复 待解决
如何调用系统相机拍照
1561浏览 • 1回复 待解决
harmonyOS基于api9如何调用相机拍照
3697浏览 • 1回复 待解决
HarmonyOS 自定义相机拍照数据展示
587浏览 • 1回复 待解决
如何调用系统拍照并获取图片
541浏览 • 1回复 待解决
如何调用系统相机,拍照、录视频?
270浏览 • 1回复 待解决
有哪位知道怎样调用系统相机拍照
1756浏览 • 1回复 待解决