title: Create File from Uri with Kotlin
body: <p>i am trying to select an image from my gallery and then uploading to my server with a form-data request</p>
  <p>this is how i start intent to select image</p>
  <pre><code>val intent = Intent()
          intent.type = &quot;image/*&quot;
          intent.action = Intent.ACTION_GET_CONTENT
          startActivityForResult(Intent.createChooser(intent, &quot;Select Picture&quot;), SELECT_PICTURE)
  </code></pre>
  <p>and this is my onActivityResult</p>
  <pre><code>override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
      super.onActivityResult(requestCode, resultCode, data)
      when (requestCode == SELECT_PICTURE &amp;&amp; resultCode == RESULT_OK &amp;&amp; data != null) {
          true -&gt; {
              data.data?.let {
                  teamImagePlaceholder?.setImageURI(it)
                  it.path?.let { path -&gt;
                      selectedImage = File(path)
                      selectedImage?.createNewFile()
                  }
              }
          }
      }
  }
  </code></pre>
  <p>the app then crashes at the line selectedImage?.createNewFile() and this is the stacktrace</p>
  <pre><code>java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=66036, result=-1, data=Intent { dat=content://com.android.providers.downloads.documents/document/raw:/storage/emulated/0/Download/download.jpeg flg=0x1 }} to activity {.MainActivity}: java.io.IOException: No such file or directory
      at android.app.ActivityThread.deliverResults(ActivityThread.java:4845)
      at android.app.ActivityThread.handleSendResult(ActivityThread.java:4886)
      at android.app.servertransaction.ActivityResultItem.execute(ActivityResultItem.java:51)
      at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
      at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016)
      at android.os.Handler.dispatchMessage(Handler.java:107)
      at android.os.Looper.loop(Looper.java:214)
      at android.app.ActivityThread.main(ActivityThread.java:7356)
      at java.lang.reflect.Method.invoke(Native Method)
      at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
   Caused by: java.io.IOException: No such file or directory
  </code></pre>
  <p>and this is the retrofit method where i make the api call</p>
  <pre><code>fun createNewTeam(teamName: String, teamTarget: String, teamDescription: String, teamImage: String?,
                    successCallback: (CreateTeamResponse?) -&gt; Unit, failureCallback: () -&gt; Unit) {
      val name = MultipartBody.Part.createFormData(&quot;team_name&quot;, null, RequestBody.create(MediaType.parse(&quot;text/plain&quot;), teamName))
      val target = MultipartBody.Part.createFormData(&quot;team_target&quot;, null, RequestBody.create(MediaType.parse(&quot;text/plain&quot;), teamTarget))
      val description = MultipartBody.Part.createFormData(&quot;team_description&quot;, null, RequestBody.create(MediaType.parse(&quot;text/plain&quot;), teamDescription))
      val image = when (teamImage) {
          null -&gt; MultipartBody.Part.createFormData(&quot;team_image&quot;, &quot;&quot;)
          else -&gt; MultipartBody.Part.createFormData(&quot;team_image&quot;, teamImage, RequestBody.create(MediaType.parse(&quot;image/*&quot;), teamImage))
      }
  
      client.createNewTeam(name, target, description, image).enqueue(object : Callback&lt;CreateTeamResponse&gt; {
          override fun onResponse(call: Call&lt;CreateTeamResponse&gt;, response: Response&lt;CreateTeamResponse&gt;) {
              successCallback(response.body())
          }
  
          override fun onFailure(call: Call&lt;CreateTeamResponse&gt;, t: Throwable) {
              failureCallback()
          }
      })
  }
  </code></pre>
  <p>retrofit interface</p>
  <pre><code>@Multipart
  @POST(&quot;api/apihere&quot;)
  fun createNewTeam(@Part teamName: MultipartBody.Part, @Part teamTarget: MultipartBody.Part,
                    @Part teamDescription: MultipartBody.Part, @Part teamImage: MultipartBody.Part): Call&lt;CreateTeamResponse&gt;
  </code></pre>
  <p>EDIT</p>
  <p>after following CommonWares answer on the other question this is my code now</p>
  <p>repo</p>
  <pre><code>fun createNewTeam(teamName: String, teamTarget: String, teamDescription: String, teamImage: Uri?,
                    contentResolver: ContentResolver, successCallback: (CreateTeamResponse?) -&gt; Unit, failureCallback: () -&gt; Unit) {
      val builder = MultipartBody.Builder()
      builder.addFormDataPart(&quot;team_name&quot;, null, RequestBody.create(MediaType.parse(&quot;text/plain&quot;), teamName))
      builder.addFormDataPart(&quot;team_target&quot;, null, RequestBody.create(MediaType.parse(&quot;text/plain&quot;), teamTarget))
      builder.addFormDataPart(&quot;team_description&quot;, null, RequestBody.create(MediaType.parse(&quot;text/plain&quot;), teamDescription))
      if(teamImage != null)
          builder.addFormDataPart(&quot;team_image&quot;, teamImage.toString(), InputStreamRequestBody(MediaType.parse(&quot;image/*&quot;)!!, contentResolver, teamImage))
      builder.setType(MultipartBody.FORM)
      val requestBody = builder.build()
  
      client.createNewTeam(requestBody).enqueue(object : Callback&lt;CreateTeamResponse&gt; {
          override fun onResponse(call: Call&lt;CreateTeamResponse&gt;, response: Response&lt;CreateTeamResponse&gt;) {
              successCallback(response.body())
          }
  
          override fun onFailure(call: Call&lt;CreateTeamResponse&gt;, t: Throwable) {
              failureCallback()
          }
      })
  }
  </code></pre>
  <p>interface</p>
  <pre><code>@POST(&quot;api/apihere&quot;)
  fun createNewTeam(@Body multipartBody: MultipartBody): Call&lt;CreateTeamResponse&gt;
  </code></pre>
  <p>i am still getting the same issue, request succeeds but the image is never uploaded</p>