title: How to get path /storage/emulated/0/Download/file_name.mime_type for Android 10 and above
body: <p>I am saving a file inside the <code>Downloads</code> directory of the device <code>(Android 11)</code> to be viewed later by my app. I'm allowing multiple file types like <code>pdf</code>, <code>word</code> etc. I was able to save the file like this: (I got this code sample from <a href="https://gitlab.com/commonsguy/download-wrangler/-/blob/master/app/src/main/java/com/commonsware/android/download/DownloadRepository.kt" rel="nofollow noreferrer">here</a>)</p>
  <pre><code>@TargetApi(29)
  private suspend fun downloadQ(
      url: String,
      filename: String,
      mimeType: String
  ) =
      withContext(Dispatchers.IO) {
          val response = ok.newCall(Request.Builder().url(url).build()).execute()
  
          if (response.isSuccessful) {
              val values = ContentValues().apply {
                  put(MediaStore.Downloads.DISPLAY_NAME, filename)
                  put(MediaStore.Downloads.MIME_TYPE, mimeType)
                  put(MediaStore.Downloads.IS_PENDING, 1)
              }
  
              val resolver = context.contentResolver
              val uri =
                  resolver.insert(MediaStore.Downloads.EXTERNAL_CONTENT_URI, values)
  
              uri?.let {
                  resolver.openOutputStream(uri)?.use { outputStream -&gt;
                      val sink = outputStream.sink().buffer()
  
                      response.body()?.source()?.let { sink.writeAll(it) }
                      sink.close()
                  }
  
                  values.clear()
                  values.put(MediaStore.Downloads.IS_PENDING, 0)
                  resolver.update(uri, values, null, null)
              } ?: throw RuntimeException(&quot;MediaStore failed for some reason&quot;)
          } else {
              throw RuntimeException(&quot;OkHttp failed for some reason&quot;)
          }
      }
  </code></pre>
  <p>But when I tried to retrieve the file, I tried with the following ways that did not work:</p>
  <pre><code>val idColumn = cursor.getColumnIndexOrThrow(MediaStore.Downloads._ID)
  val id = cursor.getLong(idColumn)
  Log.d(&quot;uri id &quot;,&quot;$id&quot;)
  val contentUri = ContentUris.withAppendedId(MediaStore.Downloads.EXTERNAL_CONTENT_URI,id)
  </code></pre>
  <p>This approach threw an exception:</p>
  <blockquote>
  <p>java.lang.IllegalArgumentException: Failed to find configured root that contains /external/downloads/78</p>
  </blockquote>
  <p>I got this <code>ID</code> (here 78) from the <code>query</code> and <code>cursor</code> from <code>ContentResolver.query()</code> and I hoped it to return the <code>Uri</code> from which I could get the File.</p>
  <p>The second approach was this:</p>
  <pre><code>val uri = MediaStore.Downloads.getContentUri(&quot;external&quot;,id)
  uri.path?.let { filePath -&gt;
  Log.d(&quot;uri path &quot;,filePath)
  val file = File(filePath)
  } ?: Log.d(&quot;uri path &quot;,&quot;null&quot;)
  </code></pre>
  <p>I used <code>external</code> as the directory based on <a href="https://stackoverflow.com/a/22069562/13343177">this</a> answer, but this approach also threw the same exception</p>
  <blockquote>
  <p>java.lang.IllegalArgumentException: Failed to find configured root that contains /external/downloads/78</p>
  </blockquote>
  <p>At the end what ended up working was hardcoding something like this after I used a file explorer app to view the exact directory path:</p>
  <pre><code>val file = File(&quot;storage/emulated/0/Download/$name.$extension&quot;)
  </code></pre>
  <p>So my question is, how do I get the value of this path dynamically, and is this path the same for all devices that can be used like this way?</p>
  <p>EDIT: I also wanted to know if I am using the <code>filename</code> and it's <code>extension</code> to view the file, then if user downloads another file with same name then how do I make sure that correct file is opened? (even if i make a separate directory for my app inside <code>Download</code>, user could still download the same file twice that has a name like <code>storage/emulated/0/Download/myDir/file(2).extension</code> )</p>