Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Implementation of OkHttp's RequestBody that supports Android's content:// URIs
import android.content.ContentResolver
import android.net.Uri
import okhttp3.MediaType
import okhttp3.RequestBody
import okio.BufferedSink
import okio.Okio
import java.lang.IllegalStateException
class ContentUriRequestBody(
private val contentResolver: ContentResolver,
private val contentUri: Uri
) : RequestBody() {
override fun contentType(): MediaType? {
val contentType = contentResolver.getType(contentUri) ?: return null
return MediaType.parse(contentType)
}
override fun writeTo(sink: BufferedSink) {
val inputStream = contentResolver.openInputStream(contentUri)
?: throw IllegalStateException("Couldn't open content URI for reading: $contentUri")
Okio.source(inputStream).use { source ->
sink.writeAll(source)
}
}
}
@neiljaywarner

This comment has been minimized.

Copy link

@neiljaywarner neiljaywarner commented May 21, 2020

awesome thanks! Not sure why this isn't built in somewhere yet, or in a blog post or extension?

@cketti

This comment has been minimized.

Copy link
Owner Author

@cketti cketti commented May 21, 2020

@neiljaywarner: What information would you hope to find in a blog post about this?

@neiljaywarner

This comment has been minimized.

Copy link

@neiljaywarner neiljaywarner commented May 21, 2020

I apologise. I didn't mean "why didn't you make a blog about it" as much as "this seems so cool why aren't more people discussing it"

@neiljaywarner

This comment has been minimized.

Copy link

@neiljaywarner neiljaywarner commented May 21, 2020

Specifically though.. Basic usage n minimum sample app or at least calling site

@cketti

This comment has been minimized.

Copy link
Owner Author

@cketti cketti commented May 23, 2020

I apologise. I didn't mean "why didn't you make a blog about it" as much as "this seems so cool why aren't more people discussing it"

@neiljaywarner: No worries, I didn't read it like that. I was asking because I was in the mood for writing. So I created this blog post and a sample app. I hope it helps.

@neiljaywarner

This comment has been minimized.

Copy link

@neiljaywarner neiljaywarner commented May 23, 2020

@neiljaywarner

This comment has been minimized.

Copy link

@neiljaywarner neiljaywarner commented May 26, 2020

ah. I didn't notice put vs post. (s3). That doesn't give me the answer yet but it does make me think your stuff is fine :)

@cketti

This comment has been minimized.

Copy link
Owner Author

@cketti cketti commented May 26, 2020

I believe the issue is that S3 doesn't support the 'chunked' transfer encoding for uploads. This is what OkHttp will use for large files when the RequestBody doesn't specify a size. You can help OkHttp out by implementing the contentLength() method. To get the size of a document you could use DocumentFile.length().

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.