Created
March 24, 2019 10:04
-
-
Save iamnaran/151b9a772cdd2e60e749f4bc8403c2d4 to your computer and use it in GitHub Desktop.
Upload Multiple Images Using Retrofit
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
// Example url :- money_expenses and response model - ServerResponse (your response success model) | |
@POST("money_expense") | |
Call<ServerResponse> createExpenses(@Body RequestBody files); |
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
// Your Presenter Class | |
public void postMoneyExpenses(RequestBody files) { | |
ApiService apiService = ApiClient.getClient().create(ApiService.class); | |
apiService.createExpenses(files).enqueue(new Callback<ServerResponse>() { | |
@Override | |
public void onResponse(@NonNull Call<ServerResponse> call, @NonNull Response<ServerResponse> response) { | |
if (response.isSuccessful()) { | |
try { | |
assert response.body() != null; | |
getView().onSuccess(response.body().getMessage()); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} else { | |
getView().onFailure(response.message()); | |
} | |
} | |
@Override | |
public void onFailure(@NonNull Call<ServerResponse> call, @NonNull Throwable throwable) { | |
super.onFailure(call, throwable); | |
getView().onFailure(super.getCause()); | |
} | |
}); | |
} |
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
// place this code in your utitlies class | |
// will help in future projects | |
// See how other paramters can be added in multiparts | |
// here doc[] is the multiple image key and other are just fields. ;) | |
// ArrayList<Uri> images is the list of images you get from onResult | |
public static MultipartBody getAddReportFiles(Context context, ArrayList<Uri> images, String amount, String topicId, String description) { | |
MultipartBody.Builder builder = new MultipartBody.Builder(); | |
builder.setType(MultipartBody.FORM); | |
builder.addFormDataPart("amount", amount); | |
builder.addFormDataPart("topic_id", topicId); | |
builder.addFormDataPart("description", description); | |
for (int i = 0; i < images.size(); i++) { | |
File file = new File(getRealPathFromUri(context, Uri.parse(String.valueOf(images.get(i))))); | |
builder.addFormDataPart("doc[]", file.getName(), RequestBody.create(MediaType.parse("multipart/form-data"), file)); | |
} | |
return builder.build(); | |
} | |
public static String getRealPathFromUri(Context context, Uri contentUri) { | |
Cursor cursor = null; | |
try { | |
String[] proj = {MediaStore.Images.Media.DATA}; | |
cursor = context.getContentResolver().query(contentUri, proj, null, null, null); | |
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); | |
cursor.moveToFirst(); | |
return cursor.getString(column_index); | |
} finally { | |
if (cursor != null) { | |
cursor.close(); | |
} | |
} | |
} |
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
// Call from your Activity/ Fragments | |
presenter.createExpensesBill(UtilitiesFunctions.getAddReportFiles(CreateExpenses.this,addedImagePaths,totalExpensesAmount,String.valueOf(spinnerExpensesTopic.getSelectedView().getTag()),expensesDescriptions)); | |
// DONE ! | |
// Tips | |
// Use FishBun image picker library , easy to use |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment