0

Hi in android i know how to send image from one activity to another and send text from text view to another,separately. But i want to know in an activity we have both text view and image view.But my need is i want to send both text and image from one activity to another.Any suggestions is welcomed. Thank you.

  • 1
    which type of image you have? bitmap or drawable? – Anjali Nov 21 '15 at 09:25
  • Bundle b = new Bundle(); b.putString("text_contents", string); b.putInt("Image View",R.drawable.image_resource); intent.putExtra(b); getIntent.getExtra.getString("text_content"); getIntent.getExtra.getInt("Image View"); – Ajay Nov 21 '15 at 10:17

1 Answers1

0

What do you mean by sending along an image? For passing a string, use Intent extras and Bundle.

In your first activity...

someButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Intent intent = new Intent(CurrentActivity.this, NextActivity.class);   
        intent.putExtra("text_contents", someTextView.getText().toString();
        startActivity(intent);
    }
});

In the second activity onCreate(), you retrieve the intent extras you passed via Bundle...

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Bundle bundle = getIntent().getExtras();
    if (bundle != null) {
        String someString = bundle.getString("text_contents");
    }
}

If your "image" is an R.drawable resource, then you could simply add that as well to the intent extras:

intent.putExtra("image_resource", R.drawable.some_image_resource);

And retrieve it from the Bundle like:

int someImageResource = bundle.getInt("image_resource");

And from there you can apply it to some ImageView:

someImageView.setImageResource(someImageResource);

EDIT: made a slight correction + if your "image" is a bitmap, then see Anjali's answer.

mjp66
  • 4,214
  • 6
  • 26
  • 31