In this blog, I will tell you how you can upload images with the help of a retrofit library in Android.
There are several methods to upload image in the server first of them is the conversation of Image in Base64 String and post that image in string format.
This file contains 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
var file = File(path) | |
var bitmap1 = BitmapFactory.decodeFile(file.absolutePath) | |
val bitmap= Bitmap.createScaledBitmap(bitmap1, 460, 460, false) | |
var baos1 = ByteArrayOutputStream() | |
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos1) | |
var b1 = baos1.toByteArray() | |
var encImage1 = Base64.encodeToString(b1, Base64.DEFAULT).replace("\n","").replace("\r","") |
The problem with the above method takes time fist you have to convert image to string and if you want to show that image again you have to convert base64 string to an image that takes a lot of time.
To simplify the problem and speed up upload speed we use multipart form value to upload images but make sure you created your serverside as form value type data.
steps to upload image-
1. As you know we get the image path when we select as an image from the android gallery. convert that to multipart formate
This file contains 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
var file =File(YOUR IMAGE PATH) | |
var requestFile=RequestBody.create(MediaType.parse("multipart/form-data"),file) | |
var pic=MultipartBody.Part.createFormData("user_img",file.name,requestFile) |
This file contains 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
interface ApiService { | |
@Multipart | |
@POST("post") | |
@Headers("Accept: application/json") | |
suspend fun postFile( | |
@Part("body") body:String, | |
@Part file:MultipartBody.Part, | |
@Part("name") name: String, | |
@Part("email") email: String | |
): Resp | |
} |
This file contains 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
var file =File(Your Img Path) | |
var requestFile=RequestBody.create(MediaType.parse("multipart/form-data"),file) | |
var pic=MultipartBody.Part.createFormData("user_img",file.name,requestFile) | |
CoroutineScope(Dispatchers.IO ).launch { | |
try { | |
val userdata = MyRetrofitBuilder.apiService.postFile("hello",pic,"nnn","dddd") | |
withContext(Dispatchers.Main) { | |
var value = userdata | |
Log.v("ssssss",value.toString()) | |
} | |
}catch (e: HttpException){ | |
Log.v("Exception",e.toString()) | |
} | |
} |
Project code- Click Me
0 Comments