convert string to bitmap android kotlin

import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.util.Base64

fun stringToBitmap(encodedString: String): Bitmap? {
    val decodedBytes: ByteArray = Base64.decode(encodedString, Base64.DEFAULT)
    return BitmapFactory.decodeByteArray(decodedBytes, 0, decodedBytes.size)
}

Explanation:

  1. Import necessary classes: android.graphics.Bitmap, android.graphics.BitmapFactory, and android.util.Base64.
  2. Create a function stringToBitmap that takes an encodedString of type String as a parameter.
  3. Decode the Base64-encoded string to a ByteArray using Base64.decode method, passing the encodedString and Base64.DEFAULT flag.
  4. Use BitmapFactory.decodeByteArray to convert the decoded byte array back into a Bitmap. Parameters passed are the decoded bytes (decodedBytes), starting index (0), and length (size of decodedBytes).
  5. Return the resulting Bitmap object or null if the conversion fails.