Home > Blockchain >  Get 3 Images from gallery and add it to image view
Get 3 Images from gallery and add it to image view

Time:03-05

I'm trying to put 3 images to ImageView. but the problem is when I pick an image from the gallery, The same image shows to all of the imageView, I'm Using startActivityForResult(intent, 3);

How can I select separate images for the separate imageView?

Here is my code for picking an image

  editImage1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            Intent intent = new Intent();
            intent.setAction(Intent.ACTION_GET_CONTENT);
            intent.setType("image/*");
            //noinspection deprecation
            startActivityForResult(intent, 3);
        }
    });

    editImage2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            Intent intent = new Intent();
            intent.setAction(Intent.ACTION_GET_CONTENT);
            intent.setType("image/*");
            //noinspection deprecation
            startActivityForResult(intent, 3);
        }
    });

    editImage3.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            Intent intent = new Intent();
            intent.setAction(Intent.ACTION_GET_CONTENT);
            intent.setType("image/*");
            //noinspection deprecation
            startActivityForResult(intent, 3);  
        }
    });             // other clicklistener also here

And Here is The code for the activity result

 @SuppressWarnings("deprecation")
@Override
public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
    super.onActivityResult(requestCode, resultCode, data);



    try {


        if (data == null){

            return;
        }


        if (data.getData() != null) {

            photoUri1 = data.getData();
            photoUri2 = data.getData();
            photoUri3 = data.getData();
            photoUri4 = data.getData();
            photoUri5 = data.getData();
            photoUri6 = data.getData();

            userImage1.setImageURI(photoUri1);
            userImage2.setImageURI(photoUri2);
            userImage3.setImageURI(photoUri3);
            petImage1.setImageURI(photoUri4);
            petImage2.setImageURI(photoUri5);
            petImage3.setImageURI(photoUri6);


        }




        Toast.makeText(getContext(), "Photo selected!", Toast.LENGTH_SHORT).show();



    }catch (Exception e){

        Toast.makeText(getContext(), "Error" e, Toast.LENGTH_SHORT).show();

    }



}

Is there any chance for picking images separately

Ref: enter image description here

CodePudding user response:

There are many ways to implement it. Maybe you can use a different requestCode for different images. You code:

            photoUri1 = data.getData();
            photoUri2 = data.getData();
            photoUri3 = data.getData();
            photoUri4 = data.getData();
            photoUri5 = data.getData();
            photoUri6 = data.getData();

            userImage1.setImageURI(photoUri1);
            userImage2.setImageURI(photoUri2);
            userImage3.setImageURI(photoUri3);
            petImage1.setImageURI(photoUri4);
            petImage2.setImageURI(photoUri5);
            petImage3.setImageURI(photoUri6);

is reading 6 times the same thing and put it in the 6 images

CodePudding user response:

It is for these reasons that the OnActivityResult method is deprecated.

use it like this using the Activity Results Api

var resultLauncherImage1 = registerForActivityResult(StartActivityForResult()) { result ->
    if (result.resultCode == Activity.RESULT_OK) {
        // There are no request codes
        val data: Intent? = result.data
        //code to put data in image 1
    }
}


var resultLauncherImage2 = registerForActivityResult(StartActivityForResult()) { result ->
    if (result.resultCode == Activity.RESULT_OK) {
        // There are no request codes
        val data: Intent? = result.data
        //code to put data in image 2
    }
}


var resultLauncherImage3 = registerForActivityResult(StartActivityForResult()) { result ->
    if (result.resultCode == Activity.RESULT_OK) {
        // There are no request codes
        val data: Intent? = result.data
        //code to put data in image 3
    }
}



fun openSomeActivityForResult() {
    val intent = Intent(this, SomeActivity::class.java)
    resultLauncherImage1 .launch(intent)
resultLauncherImage2.launch(intent)
resultLauncherImag3.launch(intent)
}
  • Related