2

This question is only about a design doubt. I would appreciate some help on it.

Basically I have 6 different ImageView's, all of them are connected to the same method called onImageViewClicked, this method will check which of the 6 ImageViews was clicked, and launch Gallery intent to pick a photo from gallery.

Now my problem is.....the result from the gallery intent comes on onActivityResult method but from there, how to assign the photo to the selected ImageView???......here the code:

Method onImageViewClicked that selects ImageView and launches Gallery:

private void onImageViewClicked(View v){

        switch (v.getId()){
            case R.id.imgvW_1:
                //something here
                break;
            case R.id.imgvW_2:
                //something here
                break;
            case R.id.imgvW_3:
                //something here
                break;
            case R.id.imgvW_4:
                //something here
                break;
            case R.id.imgvW_5:
                //something here
                break;
            case R.id.imgvW_6:
                //something here
                break;

        }

        Intent gallery = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.INTERNAL_CONTENT_URI);
        startActivityForResult(gallery, PICK_IMAGE);
    }

Method onActivityResult getting results from Intent:

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK && requestCode == PICK_IMAGE){
            imageUri = data.getData();
            //how to assign here the image to the clickedImageView?????????
        }
    }

---- EDIT ---- Thanks guys for the answers. All 4 answers from Ramees, Hardik Maru, Mohammed Atif and Luca Rossi look fine for me so....is any of them more convenient than the others??

codeKiller
  • 5,493
  • 17
  • 60
  • 115

5 Answers5

1

An easy option would be to use a different request code for each image, like

  1. R.id.imgvW_1 -> PICK_IMAGE_1 = 100
  2. R.id.imgvW_2 -> PICK_IMAGE_2 = 101
  3. ecc

Then check it on your onActivityResult


EDIT:

Yout can do it even faster by using the view id as request code

private void onImageViewClicked(View v){

        switch (v.getId()){
            case R.id.imgvW_1:
            case R.id.imgvW_2:
            case R.id.imgvW_3:
            case R.id.imgvW_4:
            case R.id.imgvW_5:
            case R.id.imgvW_6:
        Intent gallery = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.INTERNAL_CONTENT_URI);
        startActivityForResult(gallery, v.getId());
                break;

        }
    }

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK){
            imageUri = data.getData();
            switch (requestCode){
                case R.id.imgvW_1:
                case R.id.imgvW_2:
                case R.id.imgvW_3:
                case R.id.imgvW_4:
                case R.id.imgvW_5:
                case R.id.imgvW_6:
                 load image to request code imageview
                break;
        }
    }
Jameido
  • 1,344
  • 1
  • 11
  • 21
  • This answer doesn't involve storing a temporary variable in your activity, that's the main advantage; but using the "edited" version could remotely lead to requestCode concurrency issues if you use some other request codes inside that activity – Jameido Jun 13 '17 at 07:28
1

You can use Picasso lib to load image into ImageView.

Here is the line of the code you can use to load the image:

Picasso.with(context).load(imageUri/path).into(R.id.imageView);

Here is the link of Picasso: https://github.com/square/picasso

Here is the code to track which imageView will be used to show the image:

long idToUse;

private void onImageViewClicked(View v){

    switch (v.getId()){
        case R.id.imgvW_1:
            idToUse = R.id.imgvW_1;
            break;
        case R.id.imgvW_2:
            idToUse = R.id.imgvW_2;
            break;
        case R.id.imgvW_3:
            idToUse = R.id.imgvW_3;
            break;
        case R.id.imgvW_4:
            idToUse = R.id.imgvW_4;
            break;
        case R.id.imgvW_5:
            idToUse = R.id.imgvW_5;
            break;
        case R.id.imgvW_6:
            idToUse = R.id.imgvW_6;
            break;

    }

    Intent gallery = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.INTERNAL_CONTENT_URI);
    startActivityForResult(gallery, PICK_IMAGE);
}

and then you can use idToUse as a id in Picasso statement. Like this:

Picasso.with(context).load(imageUri/path).into(idToUse);
Hardik Maru
  • 661
  • 5
  • 8
1

Initialize one ImageView on top of your code Like

ImageView temp_image;

And your onImageViewClicked will become

 case R.id.imgvW_1:
            temp_image = imgvW_1
            break;
case R.id.imgvW_2:
            temp_image = imgvW_2
            break;

Finally use below code in onActivityResult method

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == RESULT_OK && requestCode == PICK_IMAGE){
        imageUri = data.getData();

       temp_image.setbackground(...);// or setbitmap based on what type you have
    }
}
Ramees
  • 68
  • 4
  • 16
1

create a member variable first

private ImageView clickedImage;

then in your clicklistener

private void onImageViewClicked(View v){
    //assuming that this method will be called only from ImageView
    this.clickedImage = (ImageView) v;
    Intent gallery = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.INTERNAL_CONTENT_URI);
    startActivityForResult(gallery, PICK_IMAGE);
}

then in your activity result

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == RESULT_OK && requestCode == PICK_IMAGE){
        imageUri = data.getData();
        //use Glide to pass the uri and load image in clickedImage or
        Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), imageUri);
        if(clickedImage != null){
             clickedImage.setBitmapDrawable(bitmap);
        }
    }
}
Mohammed Atif
  • 4,383
  • 7
  • 28
  • 57
1

Try it by setting the flag inside onImageViewClicked method. Use the following code

private String flag = "";    

private void onImageViewClicked(View v){

            switch (v.getId()){
                case R.id.imgvW_1:
                    flag="image1"  
                    //something here
                    break;
                case R.id.imgvW_2:
                     flag="image2"
                    //something here
                    break;
                case R.id.imgvW_3:
                    flag="image3"
                    //something here
                    break;
                case R.id.imgvW_4:
                    flag="image4"
                    //something here
                    break;
                case R.id.imgvW_5:
                     flag="image5"
                    //something here
                    break;
                case R.id.imgvW_6:
                    flag="image6"
                    //something here
                    break;

            }

            Intent gallery = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.INTERNAL_CONTENT_URI);
            startActivityForResult(gallery, PICK_IMAGE);
        }

onActivityResult

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK && requestCode == PICK_IMAGE){
            imageUri = data.getData();
            if(flag.equalsIgnoreCase("image1")){
               Picasso.with(context).load(imageUri).into(R.id.imageView);
            }
        }
    }
Jeffrey Rajan
  • 4,391
  • 4
  • 27
  • 37