鸿蒙跳转到图库或者文件选择图片并显示到Image控件中

本文介绍了在鸿蒙系统中,如何通过按钮点击事件跳转到图库或文件选择图片,并将所选图片显示在Image控件上的实现方法。涉及到DataAbility用于访问图片,以及需要申请ohos.permission.READ_USER_STORAGE权限。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

今天有个坛友问跳转到文件和图库如何将选择的图片读取出来在Image控件中显示,我这里分享一下解决思路。

首先当然是建个布局页面,页面上放个按钮和图片控件,如下代码

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

<?xml version="1.0" encoding="utf-8"?>

<DirectionalLayout

    xmlns:ohos="http://schemas.huawei.com/res/ohos"

    ohos:height="match_parent"

    ohos:width="match_parent"

    ohos:orientation="vertical">

 

    <Button

        ohos:id="$+id:btnChooseImg"

        ohos:height="match_content"

        ohos:width="match_content"

        ohos:text="选择图片"

        ohos:text_size="40vp"

        ohos:background_element="gray"></Button>

    <Image

        ohos:id="$+id:showChooseImg"

        ohos:height="match_content"

        ohos:width="match_content"

        ></Image>

 

</DirectionalLayout>

然后看一下后台代码,后代主要用到DataAbility去访问图片,访问图片我们需要申请ohos.permission.READ_USER_STORAGE权限,代码如下

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值