1

I have 2 activities as follows: The first activity (AddActivity.java activity_add.xml) contains Image button:

<ImageButton
    android:layout_width="match_parent"
    android:layout_height="120dp"
    android:onClick="onClickImageButton"
    android:id="@+id/imageButtonProfile"/>

The second activity: (EnlargeImageActivity.java - activity_enlarge_image.xml) contains another image button:

<ImageButton
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/enlargeImageButtonProfile"
    android:onClick="onClickImageButton"/>

When creating the second activity (EnlargeImageActivity) I want to put the image from first activity and put it in the second activity. I read here:

How can I pass an Buttom ID to other Activity

that it is not recommend to pass images between 2 activities. So how can I get the ImageButton of the first activity in the creation of the second activity ? I cant call findViewById(R.id.imageButtonProfile); because I'm getting null

I have tried to use:

ImageButton ib=(ImageButton)findViewById(R.id.imageButtonProfile);
Intent intent = new Intent(this, EnlargeImageActivity.class);
intent.putExtra(SRC_IMG_BUTTON_ID, ib.getId());
startActivity(intent);

and in EnlargeImageActivity.java:

Intent intent = getIntent();
int srcId = intent.getIntExtra(AddActivity.SRC_IMG_BUTTON_ID, 0);
ImageButton ibSrc =(ImageButton)findViewById(srcId);

but same results ibSrc is null

Community
  • 1
  • 1

2 Answers2

1

Instead of trying to pass the image, you could pass the URI of the image you want to load in the imageButton. From activity A, pass extraData containing image URI. In activity B get the extraData holding the URI and set your imageView source.
So when building intent do so :

intent.putExtra("imageUri", imageUri.toString());

From activity B :

Bundle bundle = getIntent().getExtras();
if (bundle != null) {
    String uriFromActivityA = bundle.getString("imageUri");
    if (uriFromActivityA != null) {
        Uri imageUri = Uri.parse(uriFromActivityA);
        imageButton.setImageUri(imageUri);
    }
}
HelloSadness
  • 945
  • 7
  • 17
0

Through intent you can pass the value with key, in that second activity you can get it by using that key.

Kiran Nemani
  • 67
  • 1
  • 6
  • 1
    I tired to use (ImageButton)findViewById(srcId); but Im getting null –  Nov 12 '16 at 14:33