HarmonyOS NEXT应用开发—发布图片评论

介绍

本示例将通过发布图片评论场景,介绍如何使用startAbilityForResult接口拉起相机拍照,并获取相机返回的数据。

效果图预览

使用说明

  1. 通过startAbilityForResult接口拉起相机,拍照后获取图片地址。

实现思路

  1. 创建CommentData类,实现IDataSource接口的对象,用于评论列表使用LazyForEach加载数据。源码参考CommentModel.ets。

    export class CommentData extends BasicDataSource {
    // 懒加载数据
    private comments: Array = [];

    // TODO:知识点:获取懒加载数据源的数据长度
    totalCount(): number {
    return this.comments.length;
    }

    // 获取指定数据项
    getData(index: number): Comment {
    return this.comments[index];
    }

    // TODO:知识点:存储数据到懒加载数据源中
    pushData(data: Comment): void {
    this.comments.push(data);
    AppStorage.setOrCreate('commentCount', this.totalCount());
    // 在数组头部添加数据
    this.notifyDataAdd(this.comments.length - 1);
    }
    }

  2. 点击下方输入提示框,拉起评论输入弹窗。源码参考ImageCommentView.ets。

    Text($r('app.string.text_input_hint'))
    ...
    .onClick(() => {
    if (this.dialogController != null) {
    // 打开评论输入弹窗
    this.dialogController.open();
    }
    })

3.在输入弹窗中,输入文字,点击相机按钮,拉起相机,拍照后获得返回的图片地址。源码参考CameraUtils.ets。

复制代码
Image($r('app.media.camera'))
  ...
  .onClick(async () => {
    ...
    let image: string = await cameraCapture(getContext(this) as common.UIAbilityContext);
    ...
  })
...
export async function cameraCapture(context: common.UIAbilityContext): Promise<string> {
  let result: common.AbilityResult = await context.startAbilityForResult({
    action: Constants.ACTION_PICKER_CAMERA,
    parameters: {
      'supportMultiMode': false,
      'callBundleName': context.abilityInfo.bundleName
    }
  });
  if (result.resultCode === 0) {
    let param: Record<string, Object> | undefined = result.want?.parameters;
    if (param !== undefined) {
      let resourceUri: string = param[Constants.KEY_RESULT_PICKER_CAMERA] as string;
      return resourceUri;
    }
  }
  return "";
}

4.点击发布按钮,将评论添加到列表中。

复制代码
Button($r('app.string.publish'))
  ...
  .onClick(() => {
    if (this.controller) {
      this.textInComment = this.text;
      this.imagesInComment = this.selectedImages;
      this.publish();
      this.controller.close();
      this.textInComment = "";
      this.imagesInComment = [];
    }
  })
...
publishComment(): void {
  let comment: Comment = new Comment("Kevin", this.textInComment, $r('app.media.icon'), this.imageInComment, this.getCurrentDate());
  this.commentList.pushData(comment);
}

工程结构&模块类型

复制代码
imagecomment                                    // har类型
|---components
|   |---constants
|       |---Constants.ets                       // 常量类
|   |---model
|       |---CommentModel.ets                    // 评论数据类
|       |---MockCommentData.ets                 // 生成模拟数据
|   |---utils
|       |---CameraUtils.ets                     // 拉起相机工具类
|   |---view
|       |---CommentInputDialog.ets              // 输入评论弹窗
|       |---CommentItemView.ets                 // 单个评论组件
|       |---ImageCommentView.ets                // 评论列表页
|       |---ImageListView.ets                   // 评论图片List组件

模块依赖

@ohos/routermodule(动态路由)

参考资料

UIAbilityContext

为了能让大家更好的学习鸿蒙(HarmonyOS NEXT)开发技术,这边特意整理了《鸿蒙开发学习手册》(共计890页),希望对大家有所帮助:https://qr21.cn/FV7h05

《鸿蒙开发学习手册》:

如何快速入门:https://qr21.cn/FV7h05

  1. 基本概念
  2. 构建第一个ArkTS应用
  3. ......

开发基础知识:https://qr21.cn/FV7h05

  1. 应用基础知识
  2. 配置文件
  3. 应用数据管理
  4. 应用安全管理
  5. 应用隐私保护
  6. 三方应用调用管控机制
  7. 资源分类与访问
  8. 学习ArkTS语言
  9. ......

基于ArkTS 开发:https://qr21.cn/FV7h05

  1. Ability开发
  2. UI开发
  3. 公共事件与通知
  4. 窗口管理
  5. 媒体
  6. 安全
  7. 网络与链接
  8. 电话服务
  9. 数据管理
  10. 后台任务(Background Task)管理
  11. 设备管理
  12. 设备使用信息统计
  13. DFX
  14. 国际化开发
  15. 折叠屏系列
  16. ......

鸿蒙开发面试真题(含参考答案):https://qr21.cn/FV7h05

腾讯T10级高工技术,安卓全套VIP课程全网免费送:https://qr21.cn/D2k9D5

相关推荐
hhhjhl28 分钟前
开源鸿蒙平台 KMP_CMP 三方库「Clikt」适配全流程
华为·开源·harmonyos
熊猫钓鱼>_>1 小时前
四种书签,一条时间线:一个读书 App 的记忆颗粒与它的边界案例【鸿蒙心迹】
开发语言·华为·ai编程·harmonyos·鸿蒙·openharmony·鸿蒙pc
锋行天下2 小时前
DeepSeek写Langgraph就是个大傻逼,一本正经胡说八道
前端·人工智能·后端
Goway_Hui2 小时前
【KMP for OpenHarmony】-- 在鸿蒙工程中使用 Apollo-Kotlin
华为·kotlin·harmonyos
jktantan2 小时前
每个节点 41 字节:把一棵百万节点树拆进 14 个 typed array
前端·javascript·vue.js·算法·typescript
计算机魔术师3 小时前
Anthropic官方插件被「寄生」,背后是同一类攻击又来了
前端
我命由我123453 小时前
访客系统形式(有用户概念、无用户概念)
java·开发语言·前端·javascript·python·产品运营·产品经理
key_3_feng3 小时前
评估与对齐:Agent 自我进化的护栏
前端·数据库·agent
正在走向自律3 小时前
HarmonyOS 手机/平板远程控制 Windows 电脑实践指南
智能手机·电脑·harmonyos·todesk·远程工具
LaughingZhu4 小时前
Product Hunt 热榜 2026-10-30:Empacer、Scape、Floot in ChatGPT
java·前端·数据库·人工智能·chatgpt·notion