Nitesh Kabra Nitesh Kabra - 1 year ago 84
Android Question

How to load an image in image view from gallery?

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.

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);
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
if (!(picturePath.equals(""))) {
Intent intent = new Intent();
intent.setClass(MainActivity.this, ImageInGellary.class);
intent.putExtra("picturePath", picturePath);


public class ImageInGellary extends Activity {
Button cancel;

public void onCreate(Bundle savedInstanceState) {
cancel = (Button) findViewById(;
Intent in = getIntent();
savedInstanceState = in.getExtras();
String picturePath = savedInstanceState.getString("picturePath");
ImageView imageView = (ImageView) findViewById(;
cancel.setOnClickListener(new OnClickListener() {

public void onClick(View v) {

* Intent i = new Intent(Intent.ACTION_PICK,
* android.provider.MediaStore
* startActivityForResult(i, RESULT_LOAD_IMAGE);
Intent intent = new Intent();
intent.setClass(ImageInGellary.this, MainActivity.class);



Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download