How to load an image in image view from gallery?

Nitesh Kabra picture Nitesh Kabra · Oct 23, 2012 · Viewed 75.8k times · Source

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

            }
        });
    }

}

Answer

Amit Prajapati picture Amit Prajapati · Oct 23, 2012
    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>