• Android调用相册并展示选中的图片


    调用相册

    	//定义请求码
        int PICK_PHOTO_REQUEST = 1234;
        int RESULT_CANCELED = 0;//定义取消码
    	//触发监听后调用相册
    	    findViewById(R.id.image_gallery).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //创建一个意图并开启
                startActivityForResult(new Intent(Intent.ACTION_PICK).setType("image/*"),PICK_PHOTO_REQUEST);
            }
        });
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    重写onActivityResult方法

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        //在日志中打印当前的请求码和返回码
        Log.i("TAG", "resultCode:"+resultCode);
        Log.i("TAG", "requestCode:"+requestCode);
    
        if (resultCode == RESULT_CANCELED) {
            if (requestCode==PICK_PHOTO_REQUEST)
                Toast.makeText(MainActivity.this, "没有选择任何图片", Toast.LENGTH_LONG).show();
        }
        if (requestCode == PICK_PHOTO_REQUEST) {
            if (data != null) {
                ContentResolver contentResolver = getContentResolver();
                try {
                    Bitmap targetBitmap = BitmapFactory.decodeStream(contentResolver.openInputStream(data.getData()));
                    Log.i("TAG", "从相册回传bitmap:" + targetBitmap);
                    //将相册回传的图像展示在ImageView组件imageView_test中
                    imageView_test.setImageBitmap(targetBitmap);
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                }
            }
         }
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25

    完整代码

    
    import androidx.appcompat.app.AppCompatActivity;
    import android.Manifest;
    import android.annotation.SuppressLint;
    import android.content.ContentResolver;
    import android.content.Intent;
    import android.graphics.Bitmap;
    import android.graphics.BitmapFactory;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.View;
    import android.widget.ImageView;
    import android.widget.Toast;
    
    import java.io.FileNotFoundException;
    
    public class MainActivity extends AppCompatActivity {
        //自定义一个请求码 这里我设为10010
        int PICK_PHOTO_REQUEST = 1234;
        int RESULT_CANCELED = 0;//定义取消码
        ImageView imageView_test;
        @SuppressLint("MissingInflatedId")
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            imageView_test = findViewById(R.id.imageView_test);
        findViewById(R.id.image_gallery).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //创建一个意图,这里指的是相册
                startActivityForResult(new Intent(Intent.ACTION_PICK).setType("image/*"),PICK_PHOTO_REQUEST);
            }
        });
    
        }
    
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        //在日志中打印当前的请求码和返回码
        Log.i("TAG", "resultCode:"+resultCode);
        Log.i("TAG", "requestCode:"+requestCode);
    
        if (resultCode == RESULT_CANCELED) {
            if (requestCode==PICK_PHOTO_REQUEST)
                Toast.makeText(MainActivity.this, "没有选择任何图片", Toast.LENGTH_LONG).show();
        }
        if (requestCode == PICK_PHOTO_REQUEST) {
            if (data != null) {
                ContentResolver contentResolver = getContentResolver();
                try {
                    Bitmap targetBitmap = BitmapFactory.decodeStream(contentResolver.openInputStream(data.getData()));
                    Log.i("TAG", "从相册回传bitmap:" + targetBitmap);
                    imageView_test.setImageBitmap(targetBitmap);
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                }
            }
         }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62

    效果演示

    在这里插入图片描述

  • 相关阅读:
    重要功能丨支持1688API接口接入一键跨境铺货及采购,解决跨境卖家货源烦恼!
    Leetcode - 361周赛
    sdp 协议中的packetization-mode方式和三种流传输模式
    润开鸿与蚂蚁数科达成战略合作,发布基于鸿蒙的mPaaS移动应用开发产品
    CDQ分治模板
    ElementUI之登陆+注册
    MySQL备份迁移之mydumper
    webGL开发微信小游戏
    【RSocket】使用 RSocket(二)——四种通信模式实践
    近万采集各种典故网站文章大全ACCESS\EXCEL数据库
  • 原文地址:https://blog.csdn.net/qq_44255741/article/details/133146810