Skip to main content

Convert image url into Multipart in kotlin android

In this post, 
    
 I added the code for how to convert the image url list to Multipart Body in kotlin android.Steps,
  • Convert image url (String) to File  
  • Create Request Body by using multipart/form-data MediaType from converted File
  • Finally, Create MultiPart Body using MultipartBody.Part.createFormData
private fun postImage(selectedUris: java.util.ArrayList<String>):
 ArrayList<MultipartBody.Part>  {
    var multiParts: ArrayList<MultipartBody.Part> = ArrayList<MultipartBody.Part>()
    for (i in 0 until selectedUris.size) {
        // 1. Create File using image url (String)
        val file = File(selectedUris.get(i))
        // 2. Create requestBody by using multipart/form-data MediaType from file
        val requestFile: RequestBody = RequestBody.create(MediaType.parse
       ("multipart/form-data"), file)
        // 3. Finally, Create MultipartBody using MultipartBody.Part.createFormData
        val body: MultipartBody.Part = MultipartBody.Part.createFormData(
       "image", file.name.trim(), requestFile)
        multiParts.add(body)
    }
    return multiParts
}

Comments

Popular posts from this blog

Business Card Scanner using OCR API (Text Recognizer) in Kotlin Android

In this post, I will explain about business card scanner using OCR API (Text Recognizer) in android. Before that you need to read about OCR API released by google in following link, https://developers.google.com/vision/text-overview Step 1 : We need to add following in dependencies compile 'com.google.android.gms:play-services-vision:11.0.2' Step 2 : Add Permissions for CAMERA, READ and WRITE_EXTERNAL_STORAGE Permissions for Capture and store image. After scan the image we will delete that stored image from storage. We need high quality image file for getting exact text, 's why we store the image. <uses-permission android:name= "android.permission.INTERNET" /> <uses-permission android:name= "android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-feature android:name= "android.hardware.camera" android:required= "false" /> <uses-permission android:name= "android.permission.READ_EXTERNAL_STORAG...