深知大多数程序员,想要提升技能,往往是自己摸索成长,但自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上鸿蒙开发知识点,真正体系化!
由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新
// Do something with result.
...
return;
default:
...
}
}
### 3. Matisse 开发实现
#### 3.1. 新建一个 Module
新建一个 Module,类型选择 HarmonyOS Library,模块名为 Matisse,如图
![](https://img-blog.csdnimg.cn/img_convert/c76e8ba92700bedb91c9837d1987e42f.webp?x-oss-process=image/format,png)
#### 3.2. 获得媒体库所有图片
新建一个 MatisseAbility 在其 slice 的 onStart 方法中申请权限并配制 config.json:
@Override
public void onStart(Intent intent) {
super.onStart(intent);
super.setUIContent(ResourceTable.Layout_ability_matisse);
String[] per={“ohos.permission.READ_USER_STORAGE”};
requestPermissionsFromUser(per,0);
}
为了获得媒体库所有图片(的 URI),需要定义一个 DataAbilityHelper,并设置 resultSet,代码如下:
DataAbilityHelper helper = DataAbilityHelper.creator(this);
try {
ResultSet resultSet =
helper.query(AVStorage.Images.Media.EXTERNAL_DATA_ABILITY_URI, null, null);
while (resultSet != null && resultSet.goToNextRow()) {
int mid =
resultSet.getInt(resultSet.getColumnIndexForName(AVStorage.Images.Media.ID))
;
Uri uri =
Uri.appendEncodedPathToUri(AVStorage.Images.Media.EXTERNAL_DATA_ABILITY_URI,
“” + mid);
FileDescriptor fileDescriptor = null;
fileDescriptor = helper.openFile(uri, “r”);
ImageSource.DecodingOptions decodingOptions = new ImageSource.DecodingOptions();
decodingOptions.desiredSize = new Size(180, 320);
ImageSource imageSource = ImageSource.create(fileDescriptor, null);
PixelMap pixelMap =
imageSource.createThumbnailPixelmap(decodingOptions, true);
uris.add(new UriModel(uri, pixelMap));
}
} catch (DataAbilityRemoteException | FileNotFoundException e) {
e.printStackTrace();
}
由于 Listcontainer 目前不支持 grid 布局,所以取得所有图片的 uri 后对结果进行分组:
if(uris.size()>3) {
List<List> newList = arraySplitUtil.splistList(uris, 3);
for (List uriModel : newList) {
nestedLists.add(new NestedList(uriModel));
}
}else {
nestedLists.add(new NestedList(uris));
}
#### 3.3. 新建 Listconainer 并使用嵌套方式实现相册视图:
listContainer = (ListContainer)
findComponentById(ResourceTable.Id_galley);
UltimateProvider ultimateProvider=new
UltimateProvider(nestedLists,getContext());
listContainer.setItemProvider(ultimateProvider);
这里使用了三方件 UltimateProvider 减少代码量编写。
#### 3.4. 点击事件和回调处理
使用 DependentLayout 在图片布局右上方加上 checkbox, 代码中 id\_icon 为图片:
checkbox=(Checkbox)findComponentById(ResourceTable.Id_checked);
checkbox.setChecked(false);
icon.setClickedListener(new Component.ClickedListener() {
@Override
public void onClick(Component component) {
checkbox.toggle();
// getModel().getUri()
}
});
checkbox.setCheckedStateChangedListener((view, state) -> {
if (state) {
MatisseAbilitySlice.selectedSet.add(getModel());
}else {
MatisseAbilitySlice.selectedSet.remove(getModel());
}
});
回调:在 config.json 中增加"action.pick\_pic"并在 MatisseAbility 中添加如下路由:
addActionRoute(“action.pick_pic”, MatisseAbilitySlice.class.getName());
新建使用按钮,并增加如下代码:
use.setClickedListener(new Component.ClickedListener() {
@Override
public void onClick(Component component) {
if (selectedSet.size()==0){
return;
}
else {
Intent resultIntent = new Intent();
resultIntent.setParam(“model”, (Serializable) selectedSet);
// Intent resultIntent = new Intent();
setResult(resultIntent);
}
}
});
#### 3.5. 编译 HAR 包
利用 Gradle 可以将 HarmonyOS Library 库模块构建为 HAR 包,构建 HAR 包的方法如下:
在 Gradle 构建任务中,双击 PackageDebugHar 或PackageReleaseHar 任务,构建 Debug 类型 或 Release 类型的 HAR。
待构建任务完成后,可以在工程目录中的 Matisse> bulid > outputs > har 目录中,获取生成的HAR 包。
![](https://img-blog.csdnimg.cn/img_convert/30ed24d771856a3795d1d34c9e4cb01a.webp?x-oss-process=image/format,png)
**为了能让大家更好的学习鸿蒙(HarmonyOS NEXT)开发技术,这边特意整理了《鸿蒙开发学习手册》(共计890页),希望对大家有所帮助:[`https://qr21.cn/FV7h05`]( )**
### 《鸿蒙开发学习手册》:
#### **如何快速入门:[`https://qr21.cn/FV7h05`]( )**
1. 基本概念
2. 构建第一个ArkTS应用
3. ……
**深知大多数程序员,想要提升技能,往往是自己摸索成长,但自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!**
![](https://img-blog.csdnimg.cn/direct/743b668910224b259a5ffe804fa6d0db.png)
![img](https://img-blog.csdnimg.cn/img_convert/7939ab98150e66a81352a631af502413.png)
![img](https://img-blog.csdnimg.cn/img_convert/77559f4484581c848dc0cb28ce0adabe.png)
**既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上鸿蒙开发知识点,真正体系化!**
**由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新**
**[需要这份系统化的资料的朋友,可以戳这里获取](https://bbs.csdn.net/topics/618636735)**
]
**既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上鸿蒙开发知识点,真正体系化!**
**由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新**
**[需要这份系统化的资料的朋友,可以戳这里获取](https://bbs.csdn.net/topics/618636735)**