Android 从图库中获取选定的图像到 imageview

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20197487/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 02:27:20  来源:igfitidea点击:

get selected image from gallery into imageview

androidxml

提问by amanjain4all

I am facing a problem in selecting the image from a gallery and setting it into the imageview. Suppose I have two activities; mainActivitycontaining buttons for gallery and secondactivitycontaining the imageviewin which the image has to be displayed.

我在从图库中选择图像并将其设置为imageview. 假设我有两个活动;mainActivity含按钮画廊和secondactivity包含imageview在其中的图像必须被显示。

 b1.setOnClickListener(new OnClickListener() {
    public void onClick(View v) {
    Intent i = new Intent(
    Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
    startActivityForResult(i, RESULT_LOAD_IMAGE);
   }
   });

Please give me the individual code for both....

请给我两者的单独代码....

回答by Hamad

here is the code to load an image from gallery:

这是从图库加载图像的代码:

public class ImageGalleryDemoActivity extends Activity {


    private static int RESULT_LOAD_IMAGE = 1;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Button buttonLoadImage = (Button) findViewById(R.id.buttonLoadPicture);
        buttonLoadImage.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {

                Intent i = new Intent(
                        Intent.ACTION_PICK,
                        android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

                startActivityForResult(i, RESULT_LOAD_IMAGE);
            }
        });
    }


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

            ImageView imageView = (ImageView) findViewById(R.id.imgView);
            imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));

        }


    }
}

回答by Hitesh Sahu

with Picassoit can be done in single line and You dont need to make cursor query

Picasso它可以在一行中完成,你不需要化妆cursor query

I have extended it for better understanding :-

我已经扩展它以便更好地理解:-

Pick Image

选择图像

    public void pickImage() {

        Intent intent = new Intent();
        intent.setType("image/*");
        intent.setAction(Intent.ACTION_GET_CONTENT);
        startActivityForResult(Intent.createChooser(intent, "Select Picture"), SELECT_PICTURE);

    }

Load Image

加载图片

 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);
            if (resultCode == RESULT_OK) {
                if (requestCode == SELECT_PICTURE) {

                    Uri selectedImageURI = data.getData();

                    Picasso.with(MainActivity1.this).load(selectedImageURI).noPlaceholder().centerCrop().fit()
                            .into((ImageView) findViewById(R.id.imageView1));
                }

            }
        }

回答by human

In my case work this solution

在我的情况下工作这个解决方案

 private void OpenGallery(){
    Intent getImageIntent = new Intent(Intent.ACTION_GET_CONTENT);
    getImageIntent .setType("image/*");
    startActivityForResult(getImageIntent , IMAGE_PICKER );
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode== IMAGE_PICKER  && resultCode == RESULT_OK) {
        Uri fullPhotoUri = data.getData();
        imageView.setImageURI(fullPhotoUri);
    }
}

You can share URI using putExtra("fullPhotoUri", fullPhotoUri.toString()) between activities.

您可以在活动之间使用 putExtra("fullPhotoUri", fullPhotoUri.toString()) 共享 URI。

回答by Hariharan

Try this.

尝试这个。

 b1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                Intent i = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                startActivityForResult(i, RESULT_LOAD_IMAGE);
            }
        });



    @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();
            ImageView imageView = (ImageView) findViewById(R.id.imgView);
            imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
        }
    }

Here some examples

这里有一些例子

http://viralpatel.net/blogs/pick-image-from-galary-android-app/

http://viralpatel.net/blogs/pick-image-from-galary-android-app/

http://www.coderzheaven.com/2012/04/20/select-an-image-from-gallery-in-android-and-show-it-in-an-imageview/

http://www.coderzheaven.com/2012/04/20/select-an-image-from-gallery-in-android-and-show-it-in-an-imageview/

回答by Muhammad Sheharyar

To Pick Image from gallery, use this code.

要从图库中选择图像,请使用此代码。

btn_imageSetter.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Intent intent = new Intent();
        intent.setType("image/*");
        intent.setAction(Intent.ACTION_GET_CONTENT);
        startActivityForResult(Intent.createChooser(intent, "Select Picture"), IMAGE_PICKER);

    }
});

Now use this Method to Set the image to the ImageViewer

现在使用此方法将图像设置为 ImageViewer

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

    if (requestCode == IMAGE_PICKER && resultCode == RESULT_OK && null != data) {
        try{
            final Uri uriImage = data.getData();
            final InputStream inputStream = getContentResolver().openInputStream(uriImage);
            final Bitmap imageMap = BitmapFactory.decodeStream(inputStream);
            iv_image.setImageBitmap(imageMap);
        }
        catch (FileNotFoundException e) {
            e.printStackTrace();
            Toast.makeText(context, "Image was not found", Toast.LENGTH_SHORT).show();
        }

    }


}