Search code examples
androidandroid-intentbitmapimageviewandroid-gallery

Select 1 image from gallery and display on multiple ImageViews


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


Solution

  • 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
                    }
                }
            }
        }
    }
    }