debo.stackoverflow debo.stackoverflow - 3 years ago 189
Android Question

Cant upload file through okhttp3

I get the file on activity result.Or should i say the content uri version of it.

public void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);

if(resultCode==RESULT_OK && requestCode==0){
Log.d("uri data",""+data.getData());
selectedFileUri = data.getData();

My question is how do i send this file through okhttp3 method?

The okhttp 3 method is as follows

multipart.addFilePart("data[User][user_picture]", selectedFileUri);

Thank you for your time.

Answer Source
OkHttpClient client = new OkHttpClient();

RequestBody requestBody = new MultipartBody.Builder()
    .addFormDataPart("image", "your file name.png",
        RequestBody.create(MEDIA_TYPE_PNG, new File("your absolute file path")))

Request request = new Request.Builder()
    .url("Your url")

//Check the response
try (Response response = client.newCall(request).execute()) {
  if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);


Go through this blog for URI How to Consume Content From a Uri


Get filename and path from URI from mediastore

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download