1

I have a main activity that launch the application. In the main activity I have an ImageView that will get the string of a path from a selected thumbnails from a GridView in a second activity via an intent.

This perhaps sounds strange and would be more naturally to launch the second activity first with the GridView, but this is a requirement for the task.

So I'm struggling with the difficulties how to deal with the string imageId that expects the intent from the second activity? I guess I will have to put a button to open the second activity and the GridView like a menu button, but any ideas how to deal with the intent thing? Appreciate some help!

Code in the main activity:

String imageId = i.getExtras().getString("image");

ImageView imageView = (ImageView) findViewById(R.id.full_image_view);

Bitmap bitmap =BitmapFactory.decodeFile(imageId);
imageView.setImageBitmap(bitmap);
3D-kreativ
  • 9,053
  • 37
  • 102
  • 159

1 Answers1

3

open the second activity using startActivityForResult, something like:

to start the second activity:

Intent i = new Intent(this, SecondActivity.class);
startActivityForResult(i, 1);

Then handle the returned result:

 @Override

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if(resultCode == Activity.RESULT_OK) {
       String imageId = data.getExtras().getString("image");

       ImageView imageView = (ImageView) findViewById(R.id.full_image_view);

       Bitmap bitmap =BitmapFactory.decodeFile(imageId);
       imageView.setImageBitmap(bitmap);
    }
}

In the second actitivty, when a thumbnail is selected:

 Intent intent = new Intent();
 intent.putExtra("image", path);
 setResult(RESULT_OK, intent);
 finish();

check https://stackoverflow.com/a/10407371/1434631

Community
  • 1
  • 1
Nermeen
  • 15,883
  • 5
  • 59
  • 72
  • I get a red line below the onActivityResult and the message is: void is an invalid type for the variable onActivityResult ?? – 3D-kreativ Feb 19 '13 at 09:05
  • Perhaps it was wrong to place that code within the onCreate method, better when I moved it outside! – 3D-kreativ Feb 19 '13 at 09:06
  • Almost there! I get a red line below Intent i = new Intent(this, Activity_2.class); in the onClick method! Why? – 3D-kreativ Feb 19 '13 at 09:59
  • It's working when I use getApplicationContect instead of "this" – 3D-kreativ Feb 19 '13 at 10:15
  • Your code is working but there is a strange thing. In activity 1 I have the ImageView and also a button to open activity 2 to show the GridView. But if a place the button after the ImageView in the layout file, the application force a close! What could be the reason for that? – 3D-kreativ Feb 19 '13 at 10:17
  • OK! Thanks for the help! I guess I can you this code to open other activities and pass data back. – 3D-kreativ Feb 19 '13 at 10:26