'Use MultiPart in Retrofit Get Value Error
I want to post my image to server.
I used Multipart.Part by Retrofit2
Header value is correct But Image Data value get crash value

like this value
Why this Happened??
I need to post value by form-data
I find it for very very many time....! Plz help me
here is my code
val filePath = fileUri.path
val stream = ByteArrayOutputStream()
resizeBitmap(filePath, 512, 512)?.
compress(Bitmap.CompressFormat.JPEG, 90, stream)
val file = File(filePath!!)
val fileToUpload = run {
val requestBody = file.asRequestBody("image/jpeg".toMediaTypeOrNull())
MultipartBody.Part.createFormData("profile_image", file.name, requestBody)
}
APIUserProfileImage.request(requestable, fileToUpload) {
Log.v("SUCCESS1", "SUCCESS1")
}
private fun resizeBitmap(photoPath: String?, targetW: Int, targetH: Int): Bitmap? {
val bmOptions = BitmapFactory.Options()
bmOptions.inJustDecodeBounds = true
BitmapFactory.decodeFile(photoPath, bmOptions)
val photoW = bmOptions.outWidth
val photoH = bmOptions.outHeight
var scaleFactor = 1
if (targetW > 0 || targetH > 0) {
scaleFactor = Math.min(photoW / targetW, photoH / targetH)
}
bmOptions.inJustDecodeBounds = false
bmOptions.inSampleSize = scaleFactor
return BitmapFactory.decodeFile(photoPath, bmOptions)
}
for API
@Multipart
@POST
fun requestMultipart(@Url endPoint: String, @Part file: MultipartBody.Part) : Call<String>
I found all of kinds happened
and server is working fine( + post key value is "profile_image"
Solution 1:[1]
You should use like this
@Multipart
@POST
fun requestMultipart(@Url endPoint: String, @Part profile_image: MultipartBody.Part) : Call<String>
instead of
@Multipart
@POST
fun requestMultipart(@Url endPoint: String, @Part file: MultipartBody.Part) : Call<String>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Rahul Mishra |
