学习笔记之Android调用系统相册选择图片

文章目录前言使用步骤小结参考资料前言在相册里选择图片上传也是很常见的功能了例如微信朋友圈等等。但是他们是自定义的选择器,可以选择多张图片并修改。这里我们讲一个最简单的:调用系统的相册选择一张图片并展示。另外有的读者还想到要通过相机拍照来选择图片的功能,也可以参考一下我的另一篇文章学习笔记之Android使用系统相机进行拍照使用步骤这里我是通过一个简单的demo来讲解怎么去实现这个功能。首先...
摘要由CSDN通过智能技术生成

前言

在相册里选择图片上传也是很常见的功能了例如微信朋友圈等等。但是他们是自定义的选择器,可以选择多张图片并修改。这里我们讲一个最简单的:调用系统的相册选择一张图片并展示。另外有的读者还想到要通过相机拍照来选择图片的功能,也可以参考一下我的另一篇文章学习笔记之Android使用系统相机进行拍照

使用步骤

这里我是通过一个简单的demo来讲解怎么去实现这个功能。首先看布局:

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:layout_marginEnd="52dp"
        android:layout_marginRight="52dp"
        android:text="choose"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="29dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/button"
        app:srcCompat="@mipmap/ic_launcher_round" />

很简单,就是一个按钮和一个imageView。然后接下来让我们想想这个功能怎么去实现:

首先打开相册,那么肯定要通过隐式启动相册activity;然后相册返回一个路径,我们就拿这个路径把路径上对应的照片展示出来。思路挺简单的,让我们写写看:
首先看代码:

	private Uri imageUri;
    private ImageView imageView;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
   
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        imageView = findViewById(R.id.imageView);
        Button button1 = findViewById(R.id.button2);
        button1.setOnClickListener(new View.OnClickListener() {
   
            @Override
            public void onClick
  • 6
    点赞
  • 28
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值