Created
April 22, 2022 17:23
-
-
Save carolinemusyoka/5fbb9ecfd40319b49f5717d0357a5856 to your computer and use it in GitHub Desktop.
Uploading an image using multipart in Kotlin
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
// 1. The Interface | |
// --> Two ways to do it, | |
``` | |
@Multipart | |
@POST("the_endpoint") | |
suspend fun postImage( | |
@Part profile_photo: MultipartBody.Part, | |
@Part ("profile_photo") requestBody: RequestBody | |
): UploadPhotoResponse | |
``` | |
// Or | |
``` | |
@POST("the_endpoint") | |
suspend fun updateProfile( | |
@Body body: RequestBody | |
): UpdateProfileResponse | |
``` | |
// Assuming you will have get the image uri when a user picks the image. One easy way is to convert the uri to a file, and convert the file to a | |
// a Multipart | |
// Converting the Uri to a file | |
// You can create a simple utility class | |
``` | |
class UriToFile(context: Context) { | |
private val applicationContext = context.applicationContext | |
fun getImageBody(imageUri: Uri): File { | |
val parcelFileDescriptor = applicationContext.contentResolver.openFileDescriptor( | |
imageUri, | |
"r", | |
null | |
) | |
val file = File( | |
applicationContext.cacheDir, | |
applicationContext.contentResolver.getFileName(imageUri) | |
) | |
val inputStream = FileInputStream(parcelFileDescriptor?.fileDescriptor) | |
val outputStream = FileOutputStream(file) | |
inputStream.copyTo(outputStream) | |
return file | |
} | |
} | |
@SuppressLint("Range") | |
fun ContentResolver.getFileName(uri: Uri): String { | |
var name = "" | |
val cursor = query( | |
uri, null, null, | |
null, null | |
) | |
cursor?.use { | |
it.moveToFirst() | |
name = it.getString(it.getColumnIndex(OpenableColumns.DISPLAY_NAME)) | |
} | |
return name | |
} | |
``` | |
// Calling the api | |
private fun updateUserProfile() { | |
val file = UriToFile(requireContext()).getImageBody(theUri) | |
val requestFile: RequestBody = file.asRequestBody("multipart/form-data".toMediaTypeOrNull()) | |
val builder: MultipartBody.Builder = MultipartBody.Builder().setType(MultipartBody.FORM) | |
builder.addFormDataPart("first_name", firstName) // whatever data you will pass to the the request body | |
.addFormDataPart("profile_photo", file.name, requestFile) // the profile photo | |
// make sure the name (ie profile_photo), matches your api, that is name of the key. | |
val requestBody: RequestBody = builder.build() | |
// pass the request body to make your retrofit call | |
viewModel.updateUserProfile(requestBody).observe(viewLifecycleOwner){...... | |
} | |
} | |
``` | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment