Created
December 20, 2017 14:08
-
-
Save whalemare/6783ec0ee2f837b0664194acb9f03e70 to your computer and use it in GitHub Desktop.
Retrofit 2 send file
This file contains hidden or 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
private fun buildMultipartBody(filePaths: List<Uri>): MultipartBody.Builder { | |
val builder = MultipartBody.Builder().setType(MultipartBody.FORM) | |
filePaths.forEachIndexed { i, uri -> | |
val extension = "jpeg" | |
val mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension) | |
crashlyticsLog(uri, extension, mimeType) | |
val file = getFileFromUri(uri) | |
builder.addFormDataPart( | |
"image_files[$i]", | |
"filename$i.$extension", | |
RequestBodyBuffer(MediaType.parse(mimeType), file) | |
) | |
} | |
return builder | |
} | |
override fun networkExecute(repository: Repository): Single<Response<Report>> { | |
return repository.sendReport(buildMultipartBody(imagesPaths).build()) | |
} | |
// Repository | |
override fun sendReport(build: MultipartBody): Single<Response<Report>> { | |
return restApi.postReport(build) | |
} | |
// Rest Api interface | |
@POST(REPORT) | |
fun postReport(@Body build: MultipartBody): Single<Response<Report>> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment