Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to pass extra data with Intent.ACTION_GET_CONTENT

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?

like image 276
Robert Avatar asked Nov 01 '22 10:11

Robert


1 Answers

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);
    }
like image 50
jlga Avatar answered Nov 15 '22 02:11

jlga