title: Is there a straightforward way to get the parent directory's Uri from a file Uri?
body: <p>Suppose a Uri can be either of the following:</p>
  
  <ul>
  <li>A Uri from a Storage Access Framework's DocumentFile (i.e. DocumentFile.getUri()).</li>
  <li>A Uri from a regular File (i.e. Uri.fromFile(File))</li>
  </ul>
  
  <p>It refers to a file under a directory in both situations.</p>
  
  <p>Is there a straightforward way to get its parent directory's Uri without trying each of the two to see which one works?</p>
  
  <p>[Edit]:
  Here is an example for SAF:</p>
  
  <p>Uri:</p>
  
  <pre><code>content://com.android.externalstorage.documents/tree/0000-0000%3Atest/document/0000-0000%3Atest%2Ffoo%2FMovies%2FRR%20parking%20lot%20a%202018_02_22_075101.mp4
  </code></pre>
  
  <p>getPath():</p>
  
  <blockquote>
    <p>/tree/0000-0000:test/document/0000-0000:test/foo/Movies/RR parking lot
    a 2018_02_22_075101.mp4</p>
  </blockquote>
  
  <p>getPathSegments():</p>
  
  <pre><code>0 = "tree"
  1 = "0000-0000:test" 
  2 = "document"
  3 = "0000-0000:test/foo/Movies/RR parking lot a 2018_02_22_075101.mp4"
  </code></pre>
  
  <p>The parent folder should be test/foo/Movies.</p>
  
  <p>Here is an example for a regular file:</p>
  
  <p>Uri:</p>
  
  <blockquote>
    <p>file:///storage/emulated/0/foo/Movies/RR%20parking%20lot%20a%202018_02_22_081351.mp4</p>
  </blockquote>
  
  <p>getPath():</p>
  
  <blockquote>
    <p>/storage/emulated/0/foo/Movies/RR parking lot a 2018_02_22_081351.mp4</p>
  </blockquote>
  
  <p>getPathSegments():</p>
  
  <pre><code>0 = "storage"
  1 = "emulated"
  2 = "0"
  3 = "foo"
  4 = "Movies"
  5 = "RR parking lot a 2018_02_22_081351.mp4"
  </code></pre>