Android之相册图片的选择与显示

  本篇博客是自己在学习Android开发工程中实现一个小demo,主要实现Android App中从相册中选择获取,并且显示,但是这里并没有做相应的裁剪,直接显示。

首先是Android的布局layout文件activity_main.xml文件,比较简单,代码如下:




    

 效果如下:

Android之相册图片的选择与显示_第1张图片

 接下来是图片选择的事件,代码如下:

    private Button chooseButton;
    private ImageView imageView;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        Log.d("ImagePicker", "onCreate: ");
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        imageView = findViewById(R.id.picture_set);
        chooseButton = findViewById(R.id.choose_pic);
        chooseButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                selectPicture();
            }
        });
    }


    /**
     * 从相冊选择照片(不裁切)
     */
    private void selectPicture() {
        Intent intent=new Intent();
        intent.setAction(Intent.ACTION_PICK);//Pick an item from the data
        intent.setType("image/*");//从全部图片中进行选择
        startActivityForResult(Intent.createChooser(intent, "Complete action using"), 1);
    }

 然后实现图片选择并回显的功能,代码如下:

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == RESULT_OK && requestCode == 1 && null != data) {
            decodeUri(data.getData());
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

    public void decodeUri(Uri uri) {
        ParcelFileDescriptor parcelFD = null;
        try {
            parcelFD = getContentResolver().openFileDescriptor(uri, "r");
            FileDescriptor imageSource = parcelFD.getFileDescriptor();

            // Decode image size
            BitmapFactory.Options o = new BitmapFactory.Options();
            o.inJustDecodeBounds = true;
            BitmapFactory.decodeFileDescriptor(imageSource, null, o);

            // the new size we want to scale to
            final int REQUIRED_SIZE = 1024;

            // Find the correct scale value. It should be the power of 2.
            int width_tmp = o.outWidth, height_tmp = o.outHeight;
            int scale = 1;
            while (true) {
                if (width_tmp < REQUIRED_SIZE && height_tmp < REQUIRED_SIZE) {
                    break;
                }
                width_tmp /= 2;
                height_tmp /= 2;
                scale *= 2;
            }

            // decode with inSampleSize
            BitmapFactory.Options o2 = new BitmapFactory.Options();
            o2.inSampleSize = scale;
            Bitmap bitmap = BitmapFactory.decodeFileDescriptor(imageSource, null, o2);

            imageView.setImageBitmap(bitmap);

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }finally {
            if (parcelFD != null)
                try {
                    parcelFD.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
        }

 decodeUri处理getData()得到的Uri,并显示。

demo完整的效果演示如下:

点击选择图片按钮:

Android之相册图片的选择与显示_第2张图片

 点击图片,显示结果:

Android之相册图片的选择与显示_第3张图片

 

你可能感兴趣的:(Android)