5

My question is simple. Is it possible to somehow pass extra data with Intent.ACTION_GET_CONTENT which the Activity receiving the Intent in it's onActivityResult() can read out?

For now I have tried to pass some data over with some standard putExtra() but that data disappears on it's way to my activity. Using following code to setup intent.

Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("audio/*");
intent.putExtra(CURRENT_REQUEST_CODE_TAG, getTargetRequestCode());
getActivity().startActivityForResult(intent, ADD_SIGNAL_REQUEST_CODE);

And I as I said, the extra data has disappeared when I get the intent in my activity. Is there some way of flagging or setup the intent so it would keep it's extra data or something else that I missing out?

If this isn't possible, are there any other kind of default/standard way of doing this? Note. I would not like to use `Shared Preferences´ for this.

Thanks!

Update

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    int currentRequestCode = data.getIntExtra(CURRENT_REQUEST_CODE_TAG, -1);
}

In my onActivityForResult() the getIntExtra()always returns the default value of -1, not the one I configured my intent with, which should be value 9 or 10. This tells me that the extra data has been lost, and a debuggin of the data object confirms my suspicions, the intent argument doesn't contain any extra data.

It seems like the intent I send isn't the same that get received in my onActivityResult(). I understand this behaviour because in my case the default Android file chooser is opened and let's the user chose a file of correct MIME type, and when the user selects a file I guess a new Intent is created with the URI to the selected file, but why does the extra data disappear, shouldn't it be kept?

Robert
  • 4,602
  • 4
  • 22
  • 33
  • Im curious: have You tried setting action to generic Intent? Like { Intent intent = new Intent(); intent.setAction(Intent.ACTION_GET_CONTENT); intent.putExtra(...) } , plus You might want to check question # 25622235 – brainovergrow Jan 31 '15 at 15:29
  • I recommends you to read [this answer](https://stackoverflow.com/a/22554156/3763032), or see my [full gist here](https://gist.github.com/mochadwi/032bbc39e6341ecff1deb3a2545e7c0a) @Robert – mochadwi Mar 20 '20 at 18:51

2 Answers2

2

I don´t think that you can pass your data as Extras through Intent that has action as Intent.ACTION_GET_CONTENT. But you can try do it something like this:

    Intent extraIntent = getActivity().getIntent() != null ? getActivity().getIntent().getIntent() : new Intent();
    extraIntent.putExtra(EXTRA_PHOTO_LOCATION_ID, loc.getId());
    extraIntent.putExtra(CURRENT_REQUEST_CODE_TAG, getTargetRequestCode());

    getActivity().setIntent(extraIntent);
    getActivity().startActivityForResult(intent, ADD_SIGNAL_REQUEST_CODE);

and onActivityResult you can read your data from activity`s Intent:

@Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        int currentRequestCode = getIntent().getIntExtra(CURRENT_REQUEST_CODE_TAG, -1);
    }
jlga
  • 277
  • 2
  • 10
  • this assumes that there's only 1 activity that can be started at a time. The 2nd one overrides the intent... – Gavriel Nov 13 '19 at 13:55
0

Hi there try the code below

Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("audio/*");
intent.putExtra(CURRENT_REQUEST_CODE_TAG, getTargetRequestCode());
startActivityForResult(intent, ADD_SIGNAL_REQUEST_CODE);

Now override the onActivityResult method in the fragment you call the above code

Kevin Crain
  • 1,905
  • 1
  • 19
  • 28