I'm making an app and at this point i have two different intents carrying images. I' am trying to pass those images in the same activity in imageViews.
Can anyone help? Thanks!!
My code is:
ImageButton btn_insert = (ImageButton)findViewById(R.id.btn_insert);
btn_insert.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(getApplicationContext(), ViewClothes.class);
i.putExtra(ITEM_IMAGE1, image1);
startActivity(i);
Intent i2 = new Intent(getApplicationContext() , ViewClothes.class);
i2.putExtra(ITEM_IMAGE2 , image2);
startActivity(i2);
}
});
And in the second activity:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.view_clothes);
Intent i = getIntent();
ImageView image1 = (ImageView)findViewById(R.id.im1);
image1.setImageBitmap(BitmapFactory.decodeFile(i.getStringExtra("image1")));
Intent i2 = getIntent();
ImageView image2 = (ImageView)findViewById(R.id.im2);
image2.setImageBitmap(BitmapFactory.decodeFile(i2.getStringExtra("image2")));
}
You dubbled the
Intent
and you launched the nextActivity
twice.A single Intent can have multiple objects and pass those to an Activity. One Intent is used to launch the next Activity but you can easily add several objects with it as follows:
And received all images like:
Another solution might be to use a
StringArray
for your several images. In the first Activity, you could populate the array:And pass into the Intent to retrieve it like this: