How to load an image in image view from gallery?
Asked Answered
M

3

12

I have an activity, which has a button. When I click on the button it redirects me to the image gallery. I want to show the selected image in the next activity using an image view. But it is not displaying the image. The view is off screen when the image is set.

My code for selecting image and moving on next is given below. I am using no history true in my activities.

  @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();
        if (!(picturePath.equals(""))) {
            Intent intent = new Intent();
            intent.setClass(MainActivity.this, ImageInGellary.class);
            intent.putExtra("picturePath", picturePath);
            startActivity(intent);

        }
    }
}

public class ImageInGellary extends Activity {
    Button cancel;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.load_image);
        cancel = (Button) findViewById(R.id.buttonCancelPicture);
        Intent in = getIntent();
        savedInstanceState = in.getExtras();
        String picturePath = savedInstanceState.getString("picturePath");
        ImageView imageView = (ImageView) findViewById(R.id.img_view);
        imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
        cancel.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);
                 */
                Intent intent = new Intent();
                intent.setClass(ImageInGellary.this, MainActivity.class);
                startActivity(intent);

            }
        });
    }

}
Meddlesome answered 23/10, 2012 at 4:54 Comment(4)
if i remove back history:no from manifest then it is showing .but when i remove back history no from manifest it effect other requirement. Please help.Meddlesome
if (!(picturePath.equals(""))) { Intent intent = new Intent(); intent.setClass(MainActivity.this, ImageInGellary.class); intent.putExtra("picturePath", picturePath); startActivity(intent); } this is main issue.Meddlesome
as nohistory will remove context of this activity and i cant start from this activity to image gellaryMeddlesome
You need to do something extra for Android 11. Check this answerDeutsch
H
23
    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));
             
            }
         
         
        }
    }

To support android 11 you need to add this code in AndroidMainfest.xml

        <queries>
            <intent>
                <action android:name="android.intent.action.GET_CONTENT" />
                <data android:mimeType="image/*"/>
            </intent>
        </queries>
Hagride answered 23/10, 2012 at 5:29 Comment(1)
This won't work in android 11. Check this answerDeutsch
F
9

If you want to achieve it in 1 line then all you need to do is :

Picasso.with(MainActivity.this).load(data.getData()).noPlaceholder().centerCrop().fit().into((ImageView) findViewById(R.id.imageView1));

Complete Solution :-

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

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

            //Get ImageURi and load with help of picasso
            //Uri selectedImageURI = data.getData();

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

    }
}

In your situation you need to pass ImageURI to next activity

Uri selectedImageURI = data.getData();
Farley answered 4/5, 2016 at 6:49 Comment(3)
yes its working thanks.But it fails when i select from galleryTrierarch
Picasso saves you a headache, I recommend you to use, nevertheless try to learn loading images out the ui thread from the developer documentation.Murrah
This won't work in android 11. Check this answerDeutsch
B
5
public void onClick(View view) {
    Intent gallery = new Intent(Intent.ACTION_GET_CONTENT);
    gallery.setType("image/*");
    startActivityForResult(gallery, 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){
        Uri imageUri = data.getData();
        imgView.setImageURI(imageUri);

    }
}
Bah answered 31/8, 2016 at 20:54 Comment(2)
You can make this answer better by editing it to explain how does it work.Plutocrat
This won't work in android 11. Check this answerDeutsch

© 2022 - 2024 — McMap. All rights reserved.