5

I have a Base64 String that represents a BitMap image.

I need to transform that String into a BitMap image again to use it on a ImageView in my Android app

How to do it?

Overwatch
  • 17
  • 2
  • 7
Mughira Dar
  • 83
  • 1
  • 6
  • Does this answer your question? [How to convert a Base64 string into a BitMap image to show it in a ImageView?](https://stackoverflow.com/questions/4837110/how-to-convert-a-base64-string-into-a-bitmap-image-to-show-it-in-a-imageview) – Vishal Thakkar Nov 20 '19 at 13:32
  • 1
    you can find a Java implementation, and convert it to kotlin using Android Studio's tool – Vladyslav Matviienko Nov 20 '19 at 13:48

5 Answers5

14

You can use this code to decode: -

val imageBytes = Base64.decode(base64String, Base64.DEFAULT)
val decodedImage = BitmapFactory.decodeByteArray(imageBytes, 0, imageBytes.length)
imageView.setImageBitmap(decodedImage)
Prashant Sable
  • 953
  • 6
  • 19
4

You can use the android methods

Here imageString is your base64String of image.

Here is the java code:

byte[] decodedByte = Base64.decode(imageString, Base64.DEFAULT);
Bitmap bitmap = BitmapFactory.decodeByteArray(decodedByte, 0, decodedString.length); 

Here is the kotlin code:

val decodedByte = Base64.decode(imageString, Base64.DEFAULT)
val bitmap = BitmapFactory.decodeByteArray(decodedByte, 0, decodedString.length) 

After that, you can set it into the image view

yourimage.setImageBitmap(bitmap);
Sunny
  • 2,887
  • 1
  • 14
  • 29
  • this doesn't look like `kotlin` – Vladyslav Matviienko Nov 21 '19 at 07:31
  • @VladyslavMatviienko If you paste the code in kotlin it automatically converts into the kotlin code. But I have added the code for kotlin as well. – Sunny Nov 21 '19 at 07:36
  • 2
    This kotlin code did not work for me until I changed the BitmapFactory.decodeByteArray(decodedByte, 0, decodedString.length) to BitmapFactory.decodeByteArray(decodedByte, 0, decodedString.size). decodedString doesn't have a "length" method, just the "size" method. After the change everything worked like a charm, so thank you for the code. – ljerka Nov 12 '20 at 08:57
1

From the answer above, I made it as a function.

KOTLIN: you could create a function like this

 fun decodePicString (encodedString: String): Bitmap {

    val imageBytes = Base64.decode(encodedString, Base64.DEFAULT)
    val decodedImage = BitmapFactory.decodeByteArray(imageBytes, 0, imageBytes.size)

    return decodedImage
}    

then call it and set it to your imgView like this:

val mImgView = findViewById<ImageView>(R.id.imageView)
mImageView.setImageBitmap(decodePicString(your encoded string for the picture))
0

For those who seek a general answer for Base64 conversions (not just Android):

You can make use of java.util.Base64

to encode

val imageString = Base64.getEncoder().encode(imageBytes)

to decode

val imageBytes = Base64.getDecoder().decode(imageString)
Fatih
  • 439
  • 4
  • 14
0

In Kotlin You can just use decode function for byteArray...

private fun stringToBitmap(encodedString: String): Bitmap {
        val imageBytes = decode(encodedString, DEFAULT)
        return BitmapFactory.decodeByteArray(imageBytes, 0, imageBytes.size)
    }