简体   繁体   中英

Kotlin: How to convert an image Uri to Bitmap to Base64

I've found lots of answers related tothis in Java, supposedely a Kotlin solution would be very much like Java, but as in many other things, the devil is on the details, and there are some.

I have several Uris stores in the SQLite database I'm using, now I want to send this images to an API that will catch them along with other data. I'll send the info via POST.

So now, I want to load the Uri, as I do when I use an ImageView.setImageURI() that will take the Uri, convert to Bitmap and place it in the ImageView container.

How do I convert from that Uri to a Bitmap object, and then encode it to Base64 to send it to the API, using Kotlin code?


EDIT

I'm trying with Anupam's imageFileToBase64() which seems to be exactly what I want, now I'm having a problem, I got a FileNotFoundException . This is what I'm doing.

I recover the Uri string from the database, it is a string that reads: content://media/external/images/media/109, so I convert it to an Uri

val uri = Uri.parse(uri_string)

Then I get the real path, and convert it to File

val file = File(uri.path)

Finally I call the function

val base64 = imageFileToBase64(file)

I have tried both with uri.path and uri.toString() and got the same results.

uri.path = /external/images/media/109

uri.toString() = content:/media/external/images/media/109

So I got no idea on what to pass to the function.

These are Kotlin methods for the following -
1. Get the bitmap from assets
2. Save bitmap to a file
3. Get Base64 from bitmap
4. Encode File/Image to Base64
5. Decode Base64 to File/Image

 // Get the bitmap from assets and display into image view
    val bitmap = assetsToBitmap("tulip.jpg")
    // If bitmap is not null
    bitmap?.let {
        image_view_bitmap.setImageBitmap(bitmap)
    }


    val imagePath = "C:\\base64\\image.png"

    // Encode File/Image to Base64
    val base64ImageString = encoder(imagePath)
    println("Base64ImageString = $base64ImageString")

    // Decoder Base4 to File/Image
    decoder(base64ImageString, "C:\\base64\\decoderImage.png")

    // Click listener for button widget
    button.setOnClickListener{
        if(bitmap!=null){
            // Save the bitmap to a file and display it into image view
            val uri = bitmapToFile(bitmap)
            image_view_file.setImageURI(uri)

            // Show a toast message
            toast("Bitmap saved in a file.")
        }else{
            toast("bitmap not found.")
        }
    }
}


// Method to get a bitmap from assets
private fun assetsToBitmap(fileName:String):Bitmap?{
    return try{
        val stream = assets.open(fileName)
        BitmapFactory.decodeStream(stream)
    }catch (e:IOException){
        e.printStackTrace()
        null
    }
}


// Method to save an bitmap to a file
private fun bitmapToFile(bitmap:Bitmap): Uri {
    // Get the context wrapper
    val wrapper = ContextWrapper(applicationContext)

    // Initialize a new file instance to save bitmap object
    var file = wrapper.getDir("Images",Context.MODE_PRIVATE)
    file = File(file,"${UUID.randomUUID()}.jpg")

    try{
        // Compress the bitmap and save in jpg format
        val stream:OutputStream = FileOutputStream(file)
        bitmap.compress(Bitmap.CompressFormat.JPEG,100,stream)
        stream.flush()
        stream.close()
    }catch (e:IOException){
        e.printStackTrace()
    }

    // Return the saved bitmap uri
    return Uri.parse(file.absolutePath)
}

// Method to get Base64 from bitmap
private fun imageFileToBase64(imageFile: File): String {

 return FileInputStream(imageFile).use { inputStream ->
    ByteArrayOutputStream().use { outputStream ->
        Base64OutputStream(outputStream, Base64.DEFAULT).use { base64FilterStream ->
            inputStream.copyTo(base64FilterStream)
            base64FilterStream.flush()
            outputStream.toString()
          }
       }
   }
}


// Encode File/Image to Base64   
private fun encoder(filePath: String): String{
 val bytes = File(filePath).readBytes()
 val base64 = Base64.getEncoder().encodeToString(bytes)
return base64
}


// Decode Base64 to File/Image
private fun decoder(base64Str: String, pathFile: String): Unit{
  val imageByteArray = Base64.getDecoder().decode(base64Str)
  File(pathFile).writeBytes(imageByteArray)
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM