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里面的右下角
565浏览 • 1回复 待解决
进入相册或拍照选择图片做头像
13338浏览 • 2回复 已解决
HarmonyOS 拍照返回uri转bitmap问题
335浏览 • 1回复 待解决
HarmonyOS 点击图片按钮中间没反应
419浏览 • 1回复 待解决
如何获取拍照图片地址
1835浏览 • 1回复 待解决
HarmonyOS拍照调用openSync方法报错
383浏览 • 1回复 待解决
HarmonyOS拍照saveUri异常
237浏览 • 1回复 待解决
点击FindComponentById之后没有任何反应
7461浏览 • 2回复 待解决
HarmonyOS 自定义相机拍照数据展示
513浏览 • 1回复 待解决
使用华为支付,调用收银台没有反应
386浏览 • 1回复 待解决
关于如何使用相机拍照模块拍照问题
1652浏览 • 0回复 待解决