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);
        }
    });

重写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();
            }
        }
     }
    }

完整代码


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();
            }
        }
     }
    }
}

效果演示

在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值