今天有个坛友问跳转到文件和图库如何将选择的图片读取出来在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权限,代码如下