In the sample app, I have a version of that code that uses DocumentFile to Resolver is a ContentResolver, obtained from a handy ContextĬontent is the Uri to the content to upload ![]() Type is an OkHttp MediaType identifying the MIME type of the content addFormDataPart ( "something", name, contentPart ). ![]() FORM ) // TODO add other form elements here. Val contentPart = InputStreamRequestBody ( type, resolver, content ) val requestBody = MultipartBody. In Kotlin, an equivalent implementation looks like this: ![]() Is an abstract class, so we can create our own implementation that can useĪndroid SDK classes for our own Android projects. As a result, OkHttp knows nothingĪbout Uri or ContentResolver or other Android SDK classes. If you have a content Uri, though, OkHttp alone will not be sufficient. The specific recipe in that OkHttp documentation showsĬreating one from a File, such as by using the asRequestBody() extension function For including a “file” (from the standpoint of the form), addFormDataPart()Ĭan accept a RequestBody. MultipartBody.Builder has addFormDataPart() methods that let you fill in the elements Shows how to use MultipartBody.Builder to create the body of a POST request. So, this evening I tossed togetherĪ Kotlin port of InputStreamRequestBody and its use. I realized a week ago that RequestBinĮxists and can handle this scenario. However, I never created a sample app for this, simply because I did not have a If you have a need to do a multipart form upload, and your content is identifiedīy a Uri instead of a File, the InputStreamRequestBody that Jared BurrowsĬreated (with help from Jake Wharton) is what you need. and a UriĪs changes like scoped storage increase the need to be able to work withĬontent Uri values, I have been increasingly pointing developers
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |