2

I have a code to get get a audio from storage and save the uri to the audio selected.
Also I want to pass an additional parameter TimeZoneOfDay with the intent.

here is the code to do that

  Intent intent_upload = new Intent();
  intent_upload.setType("audio/*");
  intent_upload.putExtra(TimeZoneOfDay,2);
  // Bundle b = new Bundle();
  //b.putInt(TimeZoneOfDay,2);
  intent_upload.setAction(Intent.ACTION_GET_CONTENT);
  startActivityForResult(intent_upload,1);




 @Override
    protected void onActivityResult(int requestCode,int resultCode,Intent data){

        Log.e(Tag, "Onactivity result called");
        if(requestCode == 1){
            Toast.makeText(this, "file choosen",Toast.LENGTH_SHORT).show();
            if(resultCode == RESULT_OK){
                Uri uri = data.getData();
                Bundle b = data.getExtras();

                Log.e(Tag, "dcs "+data.hasExtra(TimeZoneOfDay)+ String.valueOf(b!=null));

I also want to put an extra in my intent TimeZoneofDay. However, data.hasExtra(Timezone) is showing false and data.getExtras() is null.

How to properly pass the parameter TimeZoneOfDay ?

2 Answers2

2

How to properly pass the parameter TimeZoneOfDay ?

You don't. You did not write the app that is responding to ACTION_GET_CONTENT. You cannot force that app to put a value in an extra of the result Intent.

Instead, hold 2 in a field, and use that field in your onActivityResult(). Also, since your process might be terminated while you are in the background, hold onto that 2 in the saved instance state Bundle.

For example, in this sample app, I hold onto a File object as a field, then use that for a Uri for use with ACTION_IMAGE_CAPTURE. That way, in onActivityResult(), I know where the image should reside.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
1

You can look into this answer to check whether you aren't missing anything.

https://stackoverflow.com/a/22554156/6284141

Shubham Raitka
  • 1,034
  • 8
  • 15
  • As shown there, they are using data.getExtras().getString("NAME OF THE PARAMETER"); which will simply crash my app as data.getExtras() is null in my case. I think is that the intent has a extra TimeZoneofDay which launches the action. However, ones a audio file is selected a new intent is made with the uri of the file and thus it does not contain the extra TimeZOneOfDay. Does this make any sense ? – Debodzire Paul Apr 24 '18 at 22:56
  • in my case this is works. see here for full gists: https://gist.github.com/mochadwi/032bbc39e6341ecff1deb3a2545e7c0a – mochadwi Mar 20 '20 at 18:46