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

**URL:** <https://discuss.kotlinlang.org/t/how-to-get-an-image-bitmap-using-a-bytearray-tried-bitmap-factory/28539>\
**Category:** Support\
**Created:** [April 27, 2024, 7:42pm UTC](https://discuss.kotlinlang.org/t/how-to-get-an-image-bitmap-using-a-bytearray-tried-bitmap-factory/28539 "2024-04-27T19:42:15Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![notWorking](https://avatars.discourse-cdn.com/v4/letter/n/ebca7d/32.png) [@notWorking](https://discuss.kotlinlang.org/u/notWorking)\
**Post date:** [April 27, 2024, 7:42pm UTC](https://discuss.kotlinlang.org/t/how-to-get-an-image-bitmap-using-a-bytearray-tried-bitmap-factory/28539/1 "2024-04-27T19:42:15Z")

</div>

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

```auto
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

---

<div class="post-metadata">

**Author:** ![Skater901](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/skater901/32/10163_2.png) [@Skater901](https://discuss.kotlinlang.org/u/Skater901)\
**Post date:** [May 2, 2024, 12:15am UTC](https://discuss.kotlinlang.org/t/how-to-get-an-image-bitmap-using-a-bytearray-tried-bitmap-factory/28539/2 "2024-05-02T00:15:12Z")

</div>

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).

---

<div class="post-metadata">

**Author:** ![notWorking](https://avatars.discourse-cdn.com/v4/letter/n/ebca7d/32.png) [@notWorking](https://discuss.kotlinlang.org/u/notWorking)\
**Post date:** [May 2, 2024, 7:32am UTC](https://discuss.kotlinlang.org/t/how-to-get-an-image-bitmap-using-a-bytearray-tried-bitmap-factory/28539/3 "2024-05-02T07:32:42Z")

</div>

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.
