Last active
March 30, 2021 13:30
-
-
Save stepango/d93da8351be3f2248a800bd0877b1252 to your computer and use it in GitHub Desktop.
Kotlin Retrofit2 RequestBody from FileDescriptor
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
| fun requestBody(fd: FileDescriptor) = object : RequestBody() { | |
| override fun contentType(): MediaType = MediaType.parse("multipart/form-data") | |
| override fun writeTo(sink: BufferedSink) { | |
| Okio.source(FileInputStream(fd)).use { source -> | |
| sink.writeAll(source) | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Should not pass
FileDescriptoraround, because its ownerParcelFileDescriptor/AssetFileDescriptormust be closed whenFileDescriptorInputStreamis closed.Better way is passing
Uriand createParcelFileDescriptor/AssetFileDescriptorwhen you needInputStreamthen closeParcelFileDescriptor/AssetFileDescriptor. (AutoCloseInputStream (ParcelFileDescriptor/AssetFileDescriptor)class can be used for closing)