How to get an Image bitmap using a byteArray [tried BitMap factory]

I have a byteArray which holds the data for a jpeg image.
I am using this to get the bitmap:

try {
        val decodedBytes = Base64.decode(imageString, Base64.DEFAULT)
        if (decodedBytes.isNotEmpty()) {
            val bitMap = BitmapFactory.decodeByteArray(decodedBytes, 0, decodedBytes.size)
            Log.i("Newbitmap", decodedBytes.toString())
            Log.i("Newbitmap", decodedBytes.size.toString())
        } else {
            Log.e("Newbitmap", "Decoded bytes array is empty.")
        }
    } catch (e: Exception) {
        e.printStackTrace()
        Log.e("Newbitmap", e.toString())
    }

But I am getting null for bitMap with a debug statement: --- Failed to create image decoder with message 'invalid input'

Then I tried with a base64 String to generate the image, tested that string using some online decoders and Python, I was getting the image using online decoders and Python Pillow library. But when I use that exact same string in Android, it’s not working. I can add the base64 string here also but it has around 23k characters, so not adding right now. Let me know If anyone need that.

Thanks in advance

Dumb idea, but what about grabbing the first 10~ characters of your base64 string, decode it with your code, then compare the output to the Python decoder, for example?

Based on the problem it sounds like the Base64.decode() might not be working properly, but it could also be the BitmapFactory.decodeByteArray(), so I think the first step is to try and compare the byte output (if possible).

1 Like

Hey, thanks for the reply. Actually, I solved it yesterday. The issue was that the type of image in the base64 string was TIFF, which caused BitmapFactory.decodeByteArray to say ‘Invalid input.’ Converting the image to PNG at the source resolved it.