如何开发应用拉起系统相机拍照返回图片?希望拉起相机应用拍照后,选择图片可以返回到自己的应用

如何开发应用拉起系统相机拍照返回图片?希望拉起相机应用拍照后,选择图片可以返回到自己的应用,返回的uri为图片链接,resultCode=0,并且自身应用保持在前台状态。

async takePhoto(): Promise<string | undefined> { //打开相机 
  let context = getContext(this) as common.UIAbilityContext 
  let result = await context.startAbilityForResult({ 
    action: “ohos.want.action.imageCapture”, 
    parameters: { 
      ‘ability.params.backToOtherMissionStack’: true, 
      ‘ohos.extra.param.key.supportContinueSourceExit’: false, 
    }, 
  }) 
  return result?.want?.uri 
}

采用上述代码方法拉起相机拍照,但是选择图片后,返回的uri为空,resultCode=-1,并且自身应用自动退到后台。

HarmonyOS
2024-04-27 09:45:55
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
热辣牛奶

参考以下示例 

import common from ‘@ohos.app.ability.common’; 
@Entry 
@Component 
struct ImagePickerPage { 
  @State message: string = ‘Hello World’; 
  build() { 
    Row() { 
      Column() { 
Text(this.message) 
.fontSize(50) 
.fontWeight(FontWeight.Bold) 
 
/Button(“pickImage”).onClick(() => { 
try { 
let PhotoSelectOptions = new picker.PhotoSelectOptions(); 
PhotoSelectOptions.MIMEType = picker.PhotoViewMIMETypes.IMAGE_TYPE; 
PhotoSelectOptions.maxSelectNumber = 5; 
let photoPicker = new picker.PhotoViewPicker(); 
photoPicker.select(PhotoSelectOptions).then((PhotoSelectResult: picker.PhotoSelectResult) => { 
console.info('PhotoViewPicker.select successfully, PhotoSelectResult uri: ’ + JSON.stringify(PhotoSelectResult)); 
}).catch((err: BusinessError) => { 
console.error('PhotoViewPicker.select failed with err: ’ + JSON.stringify(err)); 
}); 
} catch (error) { 
let err: BusinessError = error as BusinessError; 
console.error('PhotoViewPicker failed with err: ’ + JSON.stringify(err)); 
} 
})/ 
Button(“camera Image”).onClick(() => { 
const context = getContext(this) as common.UIAbilityContext 
console.log(‘sss’) 
context.startAbilityForResult({ 
action:“ohos.want.action.imageCapture”, 
parameters:{ 
callBundleName:“com.hm.imageshow”//返回的应用包名 
} 
},(err,data) => { 
console.info(“context:” + ${JSON.stringify(context)}) 
console.info(“imageCapture:” + ${JSON.stringify(data)}) 
}) 
}) 
} 
      .width(‘100%’) 
    } 
    .height(‘100%’) 
  } 
}
已于2024-4-28 16:31:17修改
分享
微博
QQ
微信
回复
2024-04-28 16:30:28
相关问题
HarmonyOS 拉起相机拍照
391浏览 待解决
相机拍照界面应该怎样拉起
507浏览 • 1回复 待解决
如何调用系统相机拍照
1701浏览 • 1回复 待解决
如何调用系统相机拍照、录视频?
394浏览 • 1回复 待解决
关于如何使用相机拍照模块拍照问题
1782浏览 • 0回复 待解决