title: Error uploading file with Retrofit 2
body: <p>The system I am working has an end point that receives a file picture in order to set user's profile picture. If I try sending it via Postman, everything works fine:</p>
  
  <p><a href="https://i.stack.imgur.com/LcIXp.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/LcIXp.png" alt="enter image description here"></a></p>
  
  <p>When I try to upload it from my App using retrofit it always fail. Code + stacktrace is below:</p>
  
  <p>Server side code:</p>
  
  <pre><code>begin
           user = User.find(doorkeeper_token.resource_owner_id)
           image = params[:image]
           attachment = {
               :filename =&gt; image[:filename],
               :type =&gt; image[:type],
               :headers =&gt; image[:head],
               :tempfile =&gt; image[:tempfile]
           }
  
           image_link = ""
           if params[:upload_type] == "profile"
             user.profile_image = ActionDispatch::Http::UploadedFile.new(attachment)
             image_link = get_image_path(user.id,params[:upload_type],"original",attachment[:filename])
             user.image_profile_original = get_image_path(user.id,params[:upload_type],"original",attachment[:filename])
             user.image_profile_large = get_image_path(user.id,params[:upload_type],"large",attachment[:filename])
  </code></pre>
  
  <hr>
  
  <pre><code> /*e22*/
      @Multipart
      @POST("user/upload_image")
      fun uploadUserImage(@Header("Authorization") authHeader: String,  @Part("upload_type") description: RequestBody, @Part file: MultipartBody.Part): Call&lt;ResponseWrapper&gt;
  </code></pre>
  
  <hr>
  
  <pre><code> fun uploadUserImage(image: Uri) {
          val source = File(image.path)
          val requestFile = RequestBody.create(MediaType.parse(getContext().contentResolver.getType(image)), source)
          val body = MultipartBody.Part.createFormData("image", "profile", requestFile)
          val description = RequestBody.create(okhttp3.MultipartBody.FORM, "profile")
          mMessenger.uploadUserImage(getAuthorizationHeader(), description, body).enqueue(ResponseWrapperCallback(EventCatalog.e0022))
      }
  </code></pre>
  
  <p>Request response:</p>
  
  <pre><code>E/ResponseWrapperCallback: Failed processing request: profile: open failed: ENOENT (No such file or directory)
  E/ResponseWrapperCallback: HTTP request mapped to event e0022 finished with error
  </code></pre>
  
  <hr>
  
  <h1>Edit 1</h1>
  
  <p>I do think that this error has something to do with how the File object is being instantiated. If I create the file straight from the Uri, its real path seems not to be sent to server. I say that because of the error log above, it says that it doesn't located the file on the retrieved device's Uri. I've tried to extract file's real path from the Uri, however, error log from server has changed to:</p>
  
  <pre><code>/storage/emulated/0/Marvel/Media/Characters/Adam Warlock.jpeg: open failed: EACCES (Permission denied)
  </code></pre>
  
  <p>The way I'm instantiating the File object with the real file path is like that:</p>
  
  <pre><code>   private fun getRealPathFromURI(contentURI: Uri): String {
          val result: String
          val cursor = getContext().contentResolver.query(contentURI, null, null, null, null)
          if (cursor == null) {
              result = contentURI.path
          } else {
              cursor.moveToFirst()
              val idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA)
              result = cursor.getString(idx)
              cursor.close()
          }
          return result
      }
  
  //....
  
  val source = File(getRealPathFromURI(Uri))
  </code></pre>
  
  <h1>Edit 2</h1>
  
  <p>As per user Anurag Singh request, below is the Manifest and OS information:</p>
  
  <pre><code>    &lt;uses-permission android:name="android.permission.INTERNET" /&gt;
      &lt;uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /&gt;
      &lt;uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /&gt;
  </code></pre>
  
  <hr>
  
  <pre><code>android {
      compileSdkVersion 25
      buildToolsVersion "25.0.1"
  
      defaultConfig {
          applicationId "***"
          minSdkVersion 16
          targetSdkVersion 25
          versionCode 1
          versionName "1.0"
          testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
          multiDexEnabled true
          vectorDrawables.useSupportLibrary = true
      }
  }
  </code></pre>
  
  <hr>
  
  <h1>Edit 3</h1>
  
  <p>This is the code generated by Postman which works fine!</p>
  
  <pre><code>OkHttpClient client = new OkHttpClient();
  
  MediaType mediaType = MediaType.parse("multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW");
  
  RequestBody body = RequestBody.create(mediaType, "------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"image\"; filename=\"sample 2.jpg\"\r\nContent-Type: image/jpeg\r\n\r\n\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"upload_type\"\r\n\r\nprofile\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW--");
  
  Request request = new Request.Builder()
    .url("http://********/user/upload_image")
    .post(body)
    .addHeader("content-type", "multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW")
    .addHeader("authorization", "Bearer ******")
    .addHeader("cache-control", "no-cache")
    .addHeader("postman-token", "******")
    .build();
  
  Response response = client.newCall(request).execute();
  </code></pre>