Last active
September 19, 2022 16:51
-
-
Save shakil807g/535dc99f793d28f877db to your computer and use it in GitHub Desktop.
Multiple Image Upload Using okhttp3 android
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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 { | |
} | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
this code work well
thanks for code