本片文章是根据项目要求的思路去学习其他的博主改良的思路,思路会有雷同,但是该文确实属于本人原文创作,参考链接会在文章最后展示(啊啊啊啊啊,好官方。。。。)
先说一下项目的功能要求,类似于QQ和微信在举报\投诉用户时的界面,需要要求用户上传相关违规的聊天截图,当然在实现这个功能之前用户是已经将截图保存到手机相册里的,所以这个多图上传的功能就比较简单了,当时在做这个功能的时候本人也是从网上查找了很多资料,我一开始的思路是用代码去调系统相册,然后从系统相册去选择图片,但是运行之后发现图片只能选择一张,不能多张选择,这不符合开发逻辑(主要原因还是自己经验不足,竟然不知道还有图片选择器这个框架),发现图片选择器的相关的框架种类还是有多的的,有原生的也有大牛直接用代码手敲出来的,(但是本人更喜欢原生的,因为大牛的思路并不是人人都能理解的。。。。)所以我就不多说大牛的了,我来例举几个原生的,初级码农还是容易理解的哦,不说了,直接来给大家介绍一下吧!
布局代码:只展示显示图片的那部分代码哈
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<GridView
android:id="@+id/add_pic"
android:layout_width="200dp"
android:layout_height="fill_parent"
android:layout_marginStart="30dp"
android:background="@drawable/input_bg"
android:minHeight="56dp"
android:scrollbars="none"
android:layout_margin="10dp"
android:visibility="gone"
android:gravity="fill_horizontal"
android:stretchMode="columnWidth"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintRight_toLeftOf="@id/btn_add_pic"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
//点击该按钮跳转到系统图片选择器中
<Button
android:id="@+id/btn_add_pic"
android:layout_width="56dp"
android:layout_height="56dp"
android:layout_margin="10dp"
android:background="@drawable/add_picture_bg"
android:foreground="@mipmap/add"
android:foregroundGravity="center"
app:backgroundTint="#EFEFEF"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toEndOf="@+id/add_pic"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
<