-2

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.

debo.stackoverflow
  • 911
  • 1
  • 10
  • 30

1 Answers1

8
OkHttpClient client = new OkHttpClient();

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

Request request = new Request.Builder()
    .url("Your url")
    .post(requestBody)
    .build();

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

  System.out.println(response.body().string());
}

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

OR

Get filename and path from URI from mediastore

Community
  • 1
  • 1
Anurag Singh
  • 6,140
  • 2
  • 31
  • 47