title: Kotlin how to properly use Android Intent to save file
body: <p>I am currently trying to save a text file inside a fragment, but I can't get it to work:</p>
  
  <p>Here is the method called when the user clicks the save button</p>
  
  <pre><code>    private fun saveText(){
      val intent = Intent(Intent.ACTION_CREATE_DOCUMENT).apply {
          addCategory(Intent.CATEGORY_OPENABLE)
          type = "*/*"
          putExtra(Intent.EXTRA_TITLE, "text.txt")
      }
      startActivityForResult(intent, 1)
      }
  </code></pre>
  
  <p>Here is the onActivityResult method:</p>
  
  <pre><code>    override fun onActivityResult(requestCode: Int, resultCode: Int, resultData: Intent?) {
      if (requestCode == 1 &amp;&amp; resultCode == Activity.RESULT_OK) {
          try {
              val path = resultData?.data?.path
              Log.wtf("Path", filePath)
              val writer: Writer = BufferedWriter(FileWriter(path))
              writer.write("Example Text")
              writer.close()
          } catch (e: Exception){
              e.printStackTrace()
          }
      }
  }
  </code></pre>
  
  <p>I also have the permissions set in the manifest and the file itself is created, but nothing is written. Perhaps I'm writing to the file wrong?</p>
  
  <p>The error thrown is FileNotFoundException, because its trying to use a file from /document when I'm selecting one from /downloads</p>
  
  <p>Suggested solution which unfortunately doesn't work:</p>
  
  <pre><code>                resultData?.data?.let {
                  requireActivity().contentResolver.openOutputStream(it).use { stream -&gt;
                      stream!!.bufferedWriter().write("Example Text")
                  }
              }
  </code></pre>