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

效果演示

Android调用相册并展示选中的图片_第1张图片

你可能感兴趣的:(android)