android:从相册选择图片

一,代码:

xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#FF6600"
    android:layout_gravity="center"
    tools:context=".activity.ImageActivity">

    <ImageView
        android:id="@+id/userImage"
        android:layout_width="700px"
        android:layout_height="700px"
        android:background="#000000"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
         />

    <Button
        android:id="@+id/savebutton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="从相册选择图片"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.498"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.822" />

</androidx.constraintlayout.widget.ConstraintLayout>

java

package com.example.okdemo1.activity;

import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import androidx.activity.EdgeToEdge;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.appcompat.app.AppCompatActivity;

import com.example.okdemo1.R;

public class ImageActivity extends AppCompatActivity {
    private static final int STORAGE_PERMISSION_CODE = 101;
    private ImageView imageView;
    private ActivityResultLauncher<String> mGalleryLauncher;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_image);

        // 实例化launcher
        mGalleryLauncher = registerForActivityResult(
                // 获取内容,回调函数的参数将是一个URI
                new ActivityResultContracts.GetContent(),
                // 回调函数
                this::handleGalleryResult);

        //实例化图片
        imageView = findViewById(R.id.userImage);

        //给按钮增加点击事件
        Button myButton = findViewById(R.id.savebutton);
        myButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mGalleryLauncher != null) {
                    // launch的输入参数是泛型,对应ActivityResultLauncher<String>
                    mGalleryLauncher.launch("image/*");
                }
            }
        });
    }

    //选择图片完成后的回调
    private void handleGalleryResult(Uri imageUri) {
        if (imageUri != null) {
            try {
                imageView.setImageURI(imageUri);
                // 后续处理逻辑
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}

二,测试效果:

 

posted @ 2025-05-24 17:56  刘宏缔的架构森林  阅读(27)  评论(0)    收藏  举报