title: Sending a URI from one method to another one
body: <p>A URI is got through this known code in the following method as data.getData():</p>
  <pre><code>@Override
  protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
      super.onActivityResult(requestCode, resultCode, data);
  
      switch(requestCode) {
          case 8010:
              Log.i(&quot;Test&quot;, &quot;Result URI &quot; + data.getData());
              break;
      }
  }
  </code></pre>
  <p>Now, there is another method (e.g. <code>public void dosomething() {}</code>) in the whole code where the URI is needed.</p>
  <p>How can one get the URI, i.e. <code>data.getData()</code>, in <code>dosomething()</code>?</p>
  <p>The use of <code>data.getData()</code> was tried in <code>dosomething()</code> without success. What works easily with standard variables normally, seems not to work with Uris.</p>
  <p>ADDENDUM</p>
  <p>Here a concrete example of code:</p>
  <pre><code>public class MainActivity extends AppCompatActivity {
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
      }
  
      public void dosomething() {
          [--- here I need to get my data.getData() from onActivityResult()  (directory chosen by user in startChoose()) --- something like: mypath = data.getData();  ---]
          File imagedir = getExternalStoragePublicDirectory(mypath);
          [...]
  }
  
  
      public void startChoose(View view) {
          Intent i = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE);
          i.addCategory(Intent.CATEGORY_DEFAULT);
          startActivityForResult(Intent.createChooser(i, &quot;Choose directory&quot;), 8010);
      }
  
      @Override
      protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
          super.onActivityResult(requestCode, resultCode, data);
  
          switch(requestCode) {
              case 8010:
                  Log.i(&quot;Test&quot;, &quot;Result URI &quot; + data.getData());
                  break;
          }
      }
  }
  </code></pre>
  <p>User action:
  A user choose a directory by clicking a button, which leads to <code>startChoose()</code> with <code>onClick=&quot;startChoose&quot;</code> in the XML. <code>onActivityResult()</code> gives access to the chosen directory (<code>=data.getData()</code>) and this path shall be used in <code>dosomething()</code> but I don't succeed at getting the chosen directory (<code>=data.getData()</code>) in <code>dosomething()</code>.</p>
  <p>PS: <code>getExternalStoragePublicDirectory()</code> is deprecated but so far it can be used here (or you can update it as per API 29 if you want).</p>
  <p>Basically, what I need is that the user chooses his/her directory by clicking on a button and based of that directory, the code continue its execution in <code>dosomething()</code>.</p>
  <p>Thanks in advance.</p>