当前位置:网站首页>开源一夏|OpenHarmony如何选择图片在Image组件上显示(eTS)
开源一夏|OpenHarmony如何选择图片在Image组件上显示(eTS)
2022-08-10 23:13:00 【InfoQ】
OpenHarmony如何选择图片在Image组件上显示(eTS)
相信大家都会有这样的一个使用场景,就是将图片选择
那么,我们来看一下如何使用,
导入模块
import mediaLibrary from '@ohos.multimedia.mediaLibrary';
mediaLibrary.getMediaLibrary8+
getMediaLibrary(context: Context): MediaLibrary
获取媒体库的实例,用于访问和修改用户等个人媒体数据信息(如音频、视频、图片、文档等)。
系统能力
:SystemCapability.Multimedia.MediaLibrary.Core
参数:
startMediaSelect(deprecated)
startMediaSelect(option: MediaSelectOption): Promise<Array<string>>
启动媒体选择界面,以异步方法获取选择的媒体URI列表,使用Promise形式返回结果。
说明
: 从API Version 9开始废弃。
系统能力
:SystemCapability.Multimedia.MediaLibrary.Core
参数:
返回值:
示例:
let option = {
type : "image",
count : 2
};
mediaLibrary.getMediaLibrary().startMediaSelect(option).then((value) => {
console.log("Media resources selected.");
// Obtain the media selection value.
}).catch((err) => {
console.log("An error occurred when selecting media resources.");
});
最后附上完整代码
// @ts-nocheck
/*
* Copyright (c) 2022 JianGuo Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* @ProjectName : nutsStudy
* @FileName : file
* @Author : 坚果
* @Time : 2022/8/9 15:57
* @Description : 文件描述
*/
import mediaLibrary from '@ohos.multimedia.mediaLibrary';
@Entry
@Component
struct FileSample {
@State imgList: Array<string> = [];
build() {
Column() {
Text("测试").fontSize(80).onClick(() => {
let option = {
type: "image",
count: 2
};
mediaLibrary.getMediaLibrary().startMediaSelect(option, (err, value) => {
if (err) {
console.log("An error occurred when selecting media resources." + err);
return;
}
console.log("图片路径是:" + value);
this.imgList = value;
});
})
ForEach(this.imgList, (item: any, index?: number) => { // 循环数组创建每一个Item
Image(item) // 可以生成一个或多个子组件
.width("20%").height("20%")
})
}.width("100%").height("100%")
}
}
参考文档
媒体库管理
Image
边栏推荐
猜你喜欢
随机推荐
C194铜合金C19400铁铜合金
互联网中不可缺失的网络优化:“App与Server的交互依赖于网络”
HGAME 2022 Week2 writeup
PlaidCTF 2022 Amongst Ourselves: Shipmate writeup
Unity--URP渲染管线实战教程系列之URP摄像机核心机制剖析
HCTF 2018 WarmUP writeup
【秋招】【更新中ing】手撕代码系列
一文带你了解 ViewModel 的使用及源码解析
[Autumn Recruitment] [Updating ing] Hand Tear Code Series
CW614N铜棒CuZn39Pb3对应牌号
怼不过产品经理?因为你不懂DDD领域建模与架构设计
二叉树 | 翻转二叉树 | leecode刷题笔记
HGAME 2022 Week1 writeup
"Linux" pagoda panel set up MySQL slow query log, not walk index log
Android面试冲刺:2022全新面试题——剑指Offer(备战金九银十)
高精度乘法
Metasploit——客户端渗透
[uniapp] uniapp WeChat applet development: no such file or directory error when starting the WeChat developer tool
使用PageHelper自定义PageInfo进行分页+模糊查询
安科瑞为工业能效行动计划提供EMS解决方案-Susie 周