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

            }
        });
    }

}
Khalil M
  • 1,788
  • 2
  • 22
  • 36
Nitesh Kabra
  • 509
  • 2
  • 9
  • 17
  • 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. – Nitesh Kabra Oct 23 '12 at 05:31
  • if (!(picturePath.equals(""))) { Intent intent = new Intent(); intent.setClass(MainActivity.this, ImageInGellary.class); intent.putExtra("picturePath", picturePath); startActivity(intent); } this is main issue. – Nitesh Kabra Oct 23 '12 at 05:33
  • as nohistory will remove context of this activity and i cant start from this activity to image gellary – Nitesh Kabra Oct 23 '12 at 05:34
  • You need to do something extra for Android 11. Check [this](https://stackoverflow.com/questions/64365455/how-to-startactivityforresult-on-android-11/64410689#64410689) answer – Shreemaan Abhishek Oct 18 '20 at 07:16

3 Answers3

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>
Amit Prajapati
  • 13,525
  • 8
  • 62
  • 84
  • 1
    This won't work in android 11. Check [this](https://stackoverflow.com/questions/64365455/how-to-startactivityforresult-on-android-11/64410689#64410689) answer – Shreemaan Abhishek Oct 18 '20 at 07:17
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();
Hitesh Sahu
  • 41,955
  • 17
  • 205
  • 154
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);

    }
}
Gene Bo
  • 11,284
  • 8
  • 90
  • 137
A. A. Sumitro
  • 59
  • 1
  • 3