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) | |
} | |
} | |
} |
This comment has been minimized.
This comment has been minimized.
@neiljaywarner: What information would you hope to find in a blog post about this? |
This comment has been minimized.
This comment has been minimized.
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" |
This comment has been minimized.
This comment has been minimized.
Specifically though.. Basic usage n minimum sample app or at least calling site |
This comment has been minimized.
This comment has been minimized.
@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. |
This comment has been minimized.
This comment has been minimized.
I'm looking forward to trying it out since unfortunately it didn't work out
for me previously in my initial attempt
....sent from my phone
…On Fri, May 22, 2020, 7:43 PM cketti ***@***.***> wrote:
***@***.**** commented on this gist.
------------------------------
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 <https://github.com/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
<https://cketti.de/2020/05/23/content-uris-and-okhttp/> and a sample app
<https://github.com/cketti/OkHttpWithContentUri>. I hope it helps.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<https://gist.github.com/8ac927509787d7085a5ef8f866806f0f#gistcomment-3314989>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAXBGYMCVNWHC22AGJRDIJDRS4L3PANCNFSM4NG6TGZQ>
.
|
This comment has been minimized.
This comment has been minimized.
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 :) |
This comment has been minimized.
This comment has been minimized.
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 |
This comment has been minimized.
awesome thanks! Not sure why this isn't built in somewhere yet, or in a blog post or extension?