从图库中获取图像并在 ImageView 中显示 [英] Get Image from the Gallery and Show in ImageView

查看:38
本文介绍了从图库中获取图像并在 ImageView 中显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要通过单击按钮从图库中获取图像并将其显示到图像视图中.

I need to get an image from the gallery on a button click and show it into the imageview.

我是这样做的:

    btn_image_button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            getImageFromAlbum();
        }
    });

方法定义如下:

   private void getImageFromAlbum(){
    try{
        Intent i = new Intent(Intent.ACTION_PICK,
                android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        startActivityForResult(i, RESULT_LOAD_IMAGE);
    }catch(Exception exp){
        Log.i("Error",exp.toString());
    }
}

活动结果方法是

  @Override
  protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
        Uri selectedImage = data.getData();
        String[] filePathColumn = { MediaStore.Images.Media.DATA };

        Cursor cursor = getContentResolver().query(selectedImage,
                filePathColumn, null, null, null);
        cursor.moveToFirst();

        int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
        String picturePath = cursor.getString(columnIndex);
        cursor.close();

        try {
            bmp = getBitmapFromUri(selectedImage);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        image_view.setImageBitmap(bmp);

        //to know about the selected image width and height
        Toast.makeText(MainActivity.this, image_view.getDrawable().getIntrinsicWidth()+" & "+image_view.getDrawable().getIntrinsicHeight(), Toast.LENGTH_SHORT).show();
    }

}

<小时>

问题

我面临的问题是当图像分辨率很高时,假设图像大小为 5mp 到 13mp.它不会加载并显示在图像视图中.


The Problem

The problem I am facing is when the image resolution is high suppose that if the image size is of 5mp to 13mp. It won't loads up and show up into the image view.

但是宽度和高度较低的图片都成功加载到图片视图中了!

But the images with the low width and height are successfully loading into the image view!

有人可以告诉我代码有什么问题以及我做错了什么吗?我只想从图库中导入相机图像并在图像视图中显示它们!

Can somebody tell me any issues with the code and what I am doing wrong? I just want to import the camera images from the gallery and show them in the image view!

推荐答案

你可以试试这个.

将此代码粘贴到您的按钮点击事件中.

paste this code in your button click event.

Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
photoPickerIntent.setType("image/*");
startActivityForResult(photoPickerIntent, RESULT_LOAD_IMG);

下面的代码是你的活动结果

and below code is your on activity result

@Override
    protected void onActivityResult(int reqCode, int resultCode, Intent data) {
        super.onActivityResult(reqCode, resultCode, data);


        if (resultCode == RESULT_OK) {
            try {
                final Uri imageUri = data.getData();
                final InputStream imageStream = getContentResolver().openInputStream(imageUri);
                final Bitmap selectedImage = BitmapFactory.decodeStream(imageStream);
                image_view.setImageBitmap(selectedImage);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
                Toast.makeText(PostImage.this, "Something went wrong", Toast.LENGTH_LONG).show();
            }

        }else {
            Toast.makeText(PostImage.this, "You haven't picked Image",Toast.LENGTH_LONG).show();
        }
    }

希望对你有帮助.

这篇关于从图库中获取图像并在 ImageView 中显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆