-2
ImageView imageView = (ImageView) findViewById(R.id.Gallery);
    imageView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent galleryIntent = new Intent(Intent.ACTION_PICK,
                    android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
            // Start the Intent
            startActivityForResult(galleryIntent, RESULT_LOAD);

        }
    });

} 
   @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    // When an Image is picked
    if (requestCode == RESULT_LOAD && resultCode == RESULT_OK
            && null != data) {
        // Get the Image from data

        Uri imageUri = data.getData();
        InputStream imageStream = null;
        try {
            imageStream = getContentResolver().openInputStream(imageUri);
            ImageView imageView = (ImageView) findViewById(R.id.Gallery);
            imageView.setImageBitmap(BitmapFactory.decodeStream(imageStream));
        } catch (FileNotFoundException e) {
            // Handle the error
        } finally {
            if (imageStream != null) {
                try {
                    imageStream.close();
                } catch (IOException e) {
                    // Ignore the exception
                }
            }
        }
    }

}

This code works fine for adding and image to one imageView. How can i get it to be displayed in multiple imageViews by just selecting an image once instead of repeating the process for every imageView

Akash R
  • 83
  • 1
  • 6
  • Do you want to select multiple pictures at once form gallery? Or single picture . Make it clear . – ADM Dec 05 '17 at 09:09
  • you dont understood read properly – Divyesh Kalotra Dec 05 '17 at 09:11
  • 1
    I read it . thats why i asked . problem is not clear to me . right now seems [Duplicate](https://stackoverflow.com/questions/23426113/how-to-select-multiple-images-from-gallery-in-android). – ADM Dec 05 '17 at 09:13
  • 1
    Possible duplicate of [How to select multiple images from gallery in android?](https://stackoverflow.com/questions/23426113/how-to-select-multiple-images-from-gallery-in-android) – AskNilesh Dec 05 '17 at 09:16
  • `This code works fine for adding and image to one imageView. `. Please explain first why you have problems using it for more imageviews. – greenapps Dec 05 '17 at 09:18
  • he want to ask that how he can get multiple Selected image in activity result.you are right ADM Question may Duplicate. – Divyesh Kalotra Dec 05 '17 at 10:14
  • 1
    Dont think so. `by just selecting an image once`. No multiple images. But OP is too silent. A shame. – greenapps Dec 05 '17 at 10:25
  • I just want the user to select a single image and the selected image should be displayed in multiple imageViews – Akash R Dec 05 '17 at 11:29

2 Answers2

0
How can i get it to be displayed in multiple imageViews by just selecting an image 

Create a Bitmap from the stream.

Then assign the bitmap to as much image views you want.

greenapps
  • 11,154
  • 2
  • 16
  • 19
0

Do code like this:

 @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// When an Image is picked
if (requestCode == RESULT_LOAD && resultCode == RESULT_OK
        && null != data) {
    // Get the Image from data

    Uri imageUri = data.getData();
    InputStream imageStream = null;
    try {
        imageStream = getContentResolver().openInputStream(imageUri);
        ImageView imageView = (ImageView) findViewById(R.id.Gallery);

   imageView.setImageBitmap(BitmapFactory.decodeStream(imageStream));
   imageView1.setImageBitmap(BitmapFactory.decodeStream(imageStream));
   imageView2.setImageBitmap(BitmapFactory.decodeStream(imageStream));
   imageView3.setImageBitmap(BitmapFactory.decodeStream(imageStream));
    } catch (FileNotFoundException e) {
        // Handle the error
    } finally {
        if (imageStream != null) {
            try {
                imageStream.close();
            } catch (IOException e) {
                // Ignore the exception
            }
        }
    }
}

Here, imageView,imageView1,imageView2,imageView3 are instance of different image view.

Edited:

  @Override
 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// When an Image is picked
if (requestCode == RESULT_LOAD && resultCode == RESULT_OK
    && null != data) {
  // Get the Image from data

  Uri imageUri = data.getData();
  InputStream imageStream = null;
  try {
    imageStream = getContentResolver().openInputStream(imageUri);
     Bitmap bitmap = BitmapFactory.decodeStream(imageStream);


            imageView.setImageBitmap(bitmap);
            imageView1.setImageBitmap(bitmap);
            imageView2.setImageBitmap(bitmap);
            imageView3.setImageBitmap(bitmap);
} catch (FileNotFoundException e) {
    // Handle the error
} finally {
    if (imageStream != null) {
        try {
            imageStream.close();
        } catch (IOException e) {
            // Ignore the exception
        }
    }
}
}

Edited 2:

 public class ThirdJavaActivity extends Activity {

ImageView imageView,imageView1,imageView2,imageView3;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.content_third);

    imageView = (ImageView)findViewById(R.id.imageView);
    imageView1 = (ImageView)findViewById(R.id.imageView1);
    imageView2 = (ImageView)findViewById(R.id.imageView2);
    imageView3 = (ImageView)findViewById(R.id.imageView3); 

   Intent galleryIntent = new Intent(Intent.ACTION_PICK,
            android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
    // Start the Intent
    startActivityForResult(galleryIntent, 101);

}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
 // When an Image is picked
    if (requestCode == 101 && resultCode == RESULT_OK
            && null != data) {
        // Get the Image from data

        Uri imageUri = data.getData();
        InputStream imageStream = null;
        try {
            imageStream = getContentResolver().openInputStream(imageUri);

            Bitmap bitmap = BitmapFactory.decodeStream(imageStream);

            imageView.setImageBitmap(bitmap);
            imageView1.setImageBitmap(bitmap);
            imageView2.setImageBitmap(bitmap);
            imageView3.setImageBitmap(bitmap);
        } catch (Exception e) {
            // Handle the error
        } finally {
            if (imageStream != null) {
                try {
                    imageStream.close();
                } catch (IOException e) {
                    // Ignore the exception
                }
            }
        }
    }
}
}
halfer
  • 19,824
  • 17
  • 99
  • 186
Ankit Patidar
  • 2,731
  • 1
  • 14
  • 22