HarmonyOS 进入拍照页面,拍照后,点击右下角的 确定按钮,没有反应,不能返回原来的应用

代码如下:

/** 
 * 拍照 
 * */ 
async onTakePhotoClick() { 
  let context = getContext(this) as common.UIAbilityContext; 
 
  let wantInfo: Want = { 
    'action': 'ohos.want.action.imageCapture', 
    parameters: { 
      callBundleName: "com.xxx.xxx" //本应用bundlename 
    } 
  } 
  context.startAbilityForResult(wantInfo, (err, data) => { 
    if (err) { 
      Logger.debug("失败") 
    } else { 
      Logger.debug("信息为:" + JSON.stringify(data)) 
    } 
  }) 
}
HarmonyOS
2024-08-12 15:05:11
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
put_get

首先构造一个单例对象:

import common from '@kit.AbilityKit'; 
// 构造单例对象 
export class GlobalThis { 
  private constructor() {} 
  private static instance: GlobalThis; 
  private _uiContexts = new Map<string, common.UIAbilityContext>(); 
  public static getInstance(): GlobalThis { 
    if (!GlobalThis.instance) { 
      GlobalThis.instance = new GlobalThis(); 
    } 
    return GlobalThis.instance; 
  } 
  getContext(key: string): common.UIAbilityContext | undefined { 
    return this._uiContexts.get(key); 
  } 
  setContext(key: string, value: common.UIAbilityContext): void { 
    this._uiContexts.set(key, value); 
  } 
}

然后对EntryAbility.ets内的onCreate函数进行如下修改:

import { GlobalThis } from '../utils/GlobalThis' 
export default class EntryAbility extends UIAbility { 
  onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { 
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate'); 
    GlobalThis.getInstance().setContext("context", this.context) 
  } 
}

最后构建一个类,实现三方应用拉起系统相机并返回图像路径

import wantConstant from '@kit.AbilityKit'; 
import { GlobalThis } from './GlobalThis'; 
import Want from '@kit.AbilityKit'; 
import common from '@kit.AbilityKit'; 
export default class ThirdApp { 
  constructor() { 
  } 
  private context:common.UIAbilityContext | undefined = GlobalThis.getInstance().getContext("context"); 
  private savePath: string = getContext().filesDir; 
  public async thirdPartyCall(type: string, supportMultiMode: boolean): Promise<common.AbilityResult | undefined> { 
    console.log("thirdPartyCall " + type) 
    let imagePathSrc: string; 
    let actionName = type === "image" ? wantConstant.Action.ACTION_IMAGE_CAPTURE : wantConstant.Action.ACTION_VIDEO_CAPTURE // 拍照or录像? 
    console.log("thirdPartyCall actionName=" + actionName) // ohos.want.action.imageCapture 
    console.log("thirdPartyCall savaPath=" + this.savePath) // ohos.want.action.imageCapture 
    let want: Want = { 
      "action": actionName, 
      "parameters": { 
        supportMultiMode: supportMultiMode, 
        callBundleName: "com.example.xxx" 
      } 
    }; 
    if (this.context) { 
      let result: common.AbilityResult = await this.context.startAbilityForResult(want); 
      let params = result?.want?.parameters as Record<string, string | number> 
      imagePathSrc = params?.resourceUri as string; 
      console.info('Operation imagePathSrc= ' + imagePathSrc); 
      return result; 
    } 
    return undefined; 
  } 
} 
//index.ets 
import { common, Want } from '@kit.AbilityKit'; 
import ThirdApp from '../pages/ThirdApp' 
@Entry 
@Component 
struct Index { 
  @State message: string = 'Hello World'; 
  third = new ThirdApp() 
  build() { 
    Row() { 
      Column() { 
        Button(this.message) 
          .fontSize(50) 
          .fontWeight(FontWeight.Bold) 
          .onClick(() => { 
            let name = this.third.thirdPartyCall('image',false) 
          }) 
      } 
      .height('100%') 
    } 
  } 
}
分享
微博
QQ
微信
回复
2024-08-12 20:02:55
相关问题
怎么让text文本排在image里面的右下角
726浏览 • 1回复 待解决
HarmonyOS @ohos.net.http请求没有反应
34浏览 • 1回复 待解决
HarmonyOS 拍照无法确认返回
39浏览 • 1回复 待解决
HarmonyOS 拍照图片加水印
25浏览 • 1回复 待解决
HarmonyOS picker组件如何隐藏拍照按钮
44浏览 • 1回复 待解决
HarmonyOS 点击图片按钮中间没反应
711浏览 • 1回复 待解决
进入相册或拍照选择图片做头像
13629浏览 • 2回复 已解决
使用华为支付,调用收银台没有反应
559浏览 • 1回复 待解决