0

I have a CardView (player's CardView ) with overflow menu. The menu has a edit and delete, when you click on edit, launch a dialog and there are a lot of attributes for this player in this dialog. One of this, is a ImageView (player's photo) and when you click launch an Intent and you can pick a photo in the gallery. My question is how can I get that image and set my ImageView?

In a different thread I saw they are used startActivity and onActivityResult, but in my CardView I can't use this methods.

I cant use onActivityResult and startActivity for manage a picture that i pick from gallery

Thanks!

Marcos Vasconcelos
  • 18,136
  • 30
  • 106
  • 167
mrv
  • 31
  • 5
  • Possible duplicate of [How to pick an image from gallery (SD Card) for my app?](https://stackoverflow.com/questions/2507898/how-to-pick-an-image-from-gallery-sd-card-for-my-app) – Marcos Vasconcelos Nov 23 '17 at 18:27
  • no, because i cant use **onActivityResult** and **startActivity** for manage a picture that i pick from gallery – mrv Nov 23 '17 at 18:55

1 Answers1

0

If you can't use onActivityResult then there's a solution that may work for you.

Create a second Activity aka (PickImageActivity)

Store a ResultReceiver as argument in the Intent

Start the startActivityForResult from that Activity onCreate

 Intent intent = new Intent();
 intent.setType("image/*");
 intent.setAction(Intent.ACTION_GET_CONTENT);
 startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE);

In the onActivityResult you have something like this:

 Bitmap received = BitmapFactory.decodeStream(getContentResolver().openInputStream(data.getData()));
 Bundle result = new Bundle();
 result.putParcelable(RESULT_KEY, received);
 ((ResultReceiver) getIntent().getParcelableExtra(RECEIVER_KEY)).send(RESULT_OK, result);
 finish();

So you may start it like:

Intent pickImage = new Intent(this, PickImageActivity.class);
pickImage.putExtra(RECEIVER_KEY, new ResultReceiver() {
       public void onReceive(int resultCode, Bundle data) {
               //Work with the data received here trough RESULT_KEY
       }
});

If Bitmaps dont fit your memory do send the content file path.

This solution does work and I had implemented it in a project but refactored for activityResult cause if the previous Activity is destroyed you will not receive the Data

Marcos Vasconcelos
  • 18,136
  • 30
  • 106
  • 167