harmonyos系统图片,鸿蒙HarmonyOS-获取系统照片并解码渲染显示2(附更完整的Demo)...

a2a8f3fbf4987ec574b91f5ab62a1ccb.png

声明一下哦,本篇是接着我的上一篇文章——#2020征文-手机#获取系统照片并解码渲染显示(附完整demo) 原创 来写的。需要的可以先读读上一篇文件滴,本篇则是在上一篇代码基础上进一步修改而来。

说一下功能的升级(较上一版本):(ps:我也想搞分布式,可目前的现实不允许,还是等远程模拟器的多设备分布式联调能力开放吧)

1.没有图片会出现提示

2.相册中的所有照片都可显示,并且显示计数

3.应用随打开随刷新

不多说,先上demo跑起来的效果,如下两张图:第一张图是在手机远程模拟器中一张图片都没有时候的显示界面,第二张是自己打开远程模拟器的照相功能一顿乱点,照了N张之后的显示界面

完整的demo在附件中进行下载

c588b7366a480185c186060513c08f40.png

d7f9ce371fe8b9d837630d47ab6d80d9.png

老规矩先说升级的大概思路:

1.采用TableLayout布局实现了所有照片的显示

2.添加两个Text用来显示无照片的提示信息和照片的计数信息

3.在onActive生命周期函数中添加方法实现实时刷新

1.采用TableLayout布局实现了所有照片的显示

1.1 在布局文件中添加TableLayout布局代码,需要注意的是:这里我外边套了一层ScrollView,这是为了在图片多的时候,TableLayout可以滑动

ohos:height="600vp"

ohos:left_padding="25vp"

>

ohos:id="$+id:layout_id"

ohos:height="match_content"

ohos:width="match_parent"

>

1.2 在java代码中获取到这个布局

TableLayout img_layout;

img_layout = (TableLayout)findComponentById(ResourceTable.Id_layout_id);

img_layout.setColumnCount(3);

1.3 将新生成的图片放入布局中

Image img = new Image(MainAbilitySlice.this);

img.setId(mediaId);

img.setHeight(300);

img.setWidth(300);

img.setMarginTop(20);

img.setMarginLeft(20);

img.setPixelMap(pixelMap);

img.setScaleMode(Image.ScaleMode.ZOOM_CENTER);

img_layout.addComponent(img);

2.添加两个Text用来显示无照片的提示信息和照片的计数信息

2.1 首先在布局文件中加入两个text

ohos:id="$+id:text_pre_id"

ohos:width="match_parent"

ohos:height="match_parent"

ohos:text_alignment="center"

ohos:text_size="45fp"

ohos:text="Opening...">

ohos:id="$+id:text_id"

ohos:width="match_content"

ohos:height="match_content"

ohos:text_alignment="center"

ohos:text_size="20fp">

2.2 在java中获得这两个text组件

Text pre_text,text;

pre_text = (Text)findComponentById(ResourceTable.Id_text_pre_id);

text = (Text)findComponentById(ResourceTable.Id_text_id);

2.3 利用能不能获取到图片来判断这两个text组件的显示逻辑

if(img_ids.size() > 0){

pre_text.setVisibility(Component.HIDE);

text.setVisibility(Component.VISIBLE);

text.setText("照片数量:"+img_ids.size());

}else{

pre_text.setVisibility(Component.VISIBLE);

pre_text.setText("No picture.");

text.setVisibility(Component.HIDE);

}

3.在onActive生命周期函数中添加方法实现实时刷新

3.1 onActive生命周期函数介绍

Page会在进入INACTIVE状态后来到前台,然后系统调用此回调。Page在此之后进入ACTIVE状态,该状态是应用与用户交互的状态。所以当你把应用放到后台,打开照相机照相的时候,然后在打开此应用的时候就会调用该生命周期函数

3.2 在onActive函数中添加需要的调用

@Override

publicvoid onActive() {

super.onActive();

displayPic();

}

3.3 displayPic函数封装了整个展示图片的代码

publicvoid displayPic(){

img_layout.removeAllComponents();

ArrayList img_ids = new ArrayList();

DataAbilityHelper helper = DataAbilityHelper.creator(getContext());

try {

ResultSet result = helper.query(AVStorage.Images.Media.EXTERNAL_DATA_ABILITY_URI, null,null);

if(result == null){

pre_text.setVisibility(Component.VISIBLE);

}else{

pre_text.setVisibility(Component.HIDE);

}

while(result != null&& result.goToNextRow()){

intmediaId = result.getInt(result.getColumnIndexForName(AVStorage.Images.Media.ID));

Uri uri = Uri.appendEncodedPathToUri(AVStorage.Images.Media.EXTERNAL_DATA_ABILITY_URI,""+mediaId);

FileDescriptor filedesc = helper.openFile(uri,"r");

ImageSource.DecodingOptions decodingOpts = new ImageSource.DecodingOptions();

decodingOpts.desiredSize = new Size(300,300);

ImageSource imageSource = ImageSource.create(filedesc,null);

PixelMap pixelMap = imageSource.createThumbnailPixelmap(decodingOpts,true);

Image img = new Image(MainAbilitySlice.this);

img.setId(mediaId);

img.setHeight(300);

img.setWidth(300);

img.setMarginTop(20);

img.setMarginLeft(20);

img.setPixelMap(pixelMap);

img.setScaleMode(Image.ScaleMode.ZOOM_CENTER);

img_layout.addComponent(img);

System.out.println("xxx"+uri);

img_ids.add(mediaId);

}

}catch (DataAbilityRemoteException | FileNotFoundException e){

e.printStackTrace();

}

if(img_ids.size() > 0){

pre_text.setVisibility(Component.HIDE);

text.setVisibility(Component.VISIBLE);

text.setText("照片数量:"+img_ids.size());

}else{

pre_text.setVisibility(Component.VISIBLE);

pre_text.setText("No picture.");

text.setVisibility(Component.HIDE);

}

}

这个demo目前来说,还算基本能看。。。有时间的我会继续尝试修改完善。

有兴趣的朋友可以关注一下

完整demo的源码见附件

©著作权归作者和HarmonyOS技术社区共同所有,如需转载,请注明出处,否则将追究法律责任

【编辑推荐】

【责任编辑:jianghua TEL:(010)68476606】

点赞 0

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值