Skip to content

Instantly share code, notes, and snippets.

@shakil807g
Last active September 19, 2022 16:51
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save shakil807g/535dc99f793d28f877db to your computer and use it in GitHub Desktop.
Save shakil807g/535dc99f793d28f877db to your computer and use it in GitHub Desktop.
Multiple Image Upload Using okhttp3 android
@OnClick(R.id.fabupload)
public void upload(){
Toast.makeText(MainActivity.this, "click", Toast.LENGTH_SHORT).show();
MultipartBody.Builder builder = new MultipartBody.Builder()
.setType(MultipartBody.FORM);
for(int i = 0 ; i < images_path.size() ; i++) {
File file = new File(images_path.get(i).getThumbnailPath());
if(file.exists() {
final MediaType MEDIA_TYPE = MediaType.parse(images_path.get(i).getMimeType());
builder.addFormDataPart("my_images[]",file.getName(),RequestBody.create(MEDIA_TYPE,file));
}
else {
Log.d(TAG, "file not exist ");
}
}
builder.addFormDataPart("description", description);
builder.addFormDataPart("referer_id", doc_id);
RequestBody requestBody = builder.build();
Request request = new Request.Builder()
.url("url......")
.post(requestBody)
.build();
OkHttpClient client = new OkHttpClient.Builder().build();
Call call = client.newCall(request);
call.enqueue(new okhttp3.Callback() {
@Override
public void onFailure(Call call, IOException e) {
e.printStackTrace();
}
@Override
public void onResponse(Call call, okhttp3.Response response) throws IOException {
}
});
}
@Tarunumath
Copy link

this code work well
thanks for code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment