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
2
Answers
There are many ways to implement it. Maybe you can use a different requestCode for different images. You code:
is reading 6 times the same thing and put it in the 6 images
It is for these reasons that the OnActivityResult method is deprecated.
use it like this using the Activity Results Api