Hai Everyone!, I need one help from you guys. I w...
# android
m
Hai Everyone!, I need one help from you guys. I wants to convert the hexString to byteArray and byteArray to Bitmap. But its says like image not create with Implemented. How to do that ->?
Copy code
val img =
            "89504e470d0a1a0a0000000d49484452000000f10000005308060000004e1fe1ec000000097048597300000b1300000b1301009a9c180000382569545874584d4c3a636f6d2e61646f62652e786d7000000000003c3f787061636b657420626567696e3d22efbbbf222069643d2257354d304d7043656869487a7265537a4e54637a6b633964223f3e0a3c783a786d706d65746120786d6c6e733a783d2261646f62653a6e733a6d6574612f2220783a786d70746b3d2241646f626520584d5020436f726520352e362d633133382037392e3135393832342c20323031362f30392f31342d30313a30393a30312020202020202020223e0a2020203c7264663a52444620786d6c6e733a7264663d22687474703a2f2f7777772e77332e6f72672f313939392f30322f32322d7264662d73796e7461782d6e7323223e0a2020202020203c7264663a4465736372697074696f6e207264663a61626f75743d22220a202020202020202020202020786d6c6e733a786d703d22687474703a2f2f6e732e61646f62652e636f6d2f7861702f312e302f220a202020202020202020202020786d6c6e733a64633d22687474703a2f2f7075726c2e6f72672f64632f656c656d656e74732f312e312f220a202020202020202020202020786d6c6e733a70686f746f73686f703d22687474703a2f2f6e732e61646f62652e636f6d2f70686f746f73686f702f312e302f220a202020202020202020202020786d6c6e733a786d704d4d3d22687474703a2f2f6e732e61646f62652e636f6d2f7861702f312e302f6d6d2f220a202020202020202020202020786d6c6e733a73744576743d22687474703a2f2f6e732e61646f62652e636f6d2f7861702f312e302f73547970652f5265736f757263654576656e7423220a202020202020202020202020786d6c6e733a746966663d22687474703a2f2f6e732e61646f62652e636f6d2f746966662f312e302f220a202020202020202020202020786d6c6e733a657869663d22687474703a2f2f6e732e61646f62652e636f6d2f657869662f312e302f223e0a2020202020202020203c786d703a43726561746f72546f6f6c3e41646f62652050686f746f73686f702043432032303137202857696e646f7773293c2f786d703a43726561746f72546f6f6c3e0a2020202020202020203c786d703a437265617465446174653e323031382d31312d30365431313a31383a33342b30313a30303c2f786d703a437265617465446174653e0a2020202020202020203c786d703a4d6f64696679446174653e323031382d31312d30365431313a32323a34372b30313a30303c2f786d703a4d6f64696679446174653e0a2020202020202020203c786d703a4d65746164617461446174653e323031382d31312d30365431313a32323a34372b30313a30303c2f786d703a4d65746164617461446174653e0a2020202020202020203c64633a666f726d61743e696d6167652f706e673c2f64633a666f726d61743e0a2020202020202020203c70686f746f73686f703a436f6c6f724d6f64653e333c2f70686f746f73686f703a436f6c6f724d6f64653e0a2020202020202020203c786d704d4d3a496e7374616e636549443e786d702e6969643a31363365633637342d376436322d613434652d623566382d6531303363666362333637313c2f786d704d4d3a496e7374616e636549443e0a2020202020202020203c786d704d4d3a446f63756d656e7449443e786d702e6469643a31363365633637342d376436322d613434652d623566382d6531303363666362333637313c2f786d704d4d3a446f63756d656e7449443e0a2020202020202020203c786d704d4d3a4f726967696e616c446f63756d656e7449443e786d702e6469643a31363365633637342d376436322d613434652d623566382d6531303363666362333637313c2f786d704d4d3a4f726967696e616c446f63756d656e7449443e0a2020202020202020203c786d704d4d3a486973746f72793e0a2020202020202020202020203c7264663a5365713e0a2020202020202020202020202020203c7264663a6c69207264663a7061727365547970653d225265736f75726365223e0a2020202020202020202020202020202020203c73744576743a616374696f6e3e637265617465643c2f73744576743a616374696f6e3e0a2020202020202020202020202020202020203c73744576743a696e7374616e636549443e786d702e6969643a31363365633637342d376436322d613434652d623566382d6531303363666362333637313c2f73744576743a696e7374616e636549443e0a2020202020202020202020202020202020203c73744576743a7768656e3e323031382d31312d30365431313a31383a33342b30313a30303c2f73744576743a7768656e3e0a2020202020202020202020202020202020203c73744576743a736f6674776172654167656e743e41646f62652050686f746f73686f702043432032303137202857696e646f7773293c2f73744576743a736f6674776172654167656e743e0a2020202020202020202020202020203c2f7264663a6c693e0a2020202020202020202020203c2f7264663a5365713e0a2020202020202020203c2f786d704d4d3a48697"
        binding.btn1.setOnClickListener {
            Log.i("btn", "clicked")
            runBlocking { 
                val bitImg = convertStringToBitmapK(img2)
                binding.btn1.setImageBitmap(bitImg)
            }
        }
    }

    /**
     * * Converting Byte String to Bitmap file*/
    private suspend fun convertStringToBitmapK(hexString: String): Bitmap? {
        val IOScope = CoroutineScope(<http://Dispatchers.IO|Dispatchers.IO> + SupervisorJob())

        val decodedBytes = IOScope.async { hexStringToByteArray(hexString) }
        return decodedBytes.await()
            ?.let { BitmapFactory.decodeByteArray(decodedBytes.await(), 0, it.size) }
    }

    private fun hexStringToByteArray(hexString: String): ByteArray? {
        val len = hexString.length
        val data = ByteArray(len / 2)
        var i = 0
        while (i < len) {
            data[i / 2] = ((hexString[i].digitToIntOrNull(16)
                ?: (-1 shl 4)) + hexString[i + 1].digitToIntOrNull(16)!!).toByte()
            i += 2
        }
        return data
    }
👀 1
🤔 1
🧵 5
🫥 2
Someone Can help me in that?
🧵 1
e
Not a critical comment - your coroutine usage is complicated and could be simplified. Do you have algorithm code that makes string from the byte array? I wonder how it was done.
m
Hmm, Actually i'm beginner in android and Kotlin. And your terms i couldn't understand. Can you teach me that in short and simple? if you have a time.
🧵 2
e
Please uncheck checkbox for the message in thread that send messages to the originated channel
My question was - where did you get that string?
m
Yes, that String i got from some other website
e
Can you share which website? I want to find the algorithm how that string is formed
m
sorry its a random String generated from some website. I did not remember which was the site name.
c
How would a random string be an image? You can check the bytes after decoding to byte array for Magic Numbers and check which format this “Image” might be. https://gist.github.com/leommoore/f9e57ba2aa4bf197ebc5#
m
oops, random strings can't be image. but this string encoded to hex string from some image what i have. its really an image only. leave that weather image or not debuts... come to how to convert it to again byte array and to bitmap.
c
https://stackoverflow.com/questions/66613717/kotlin-convert-hex-string-to-bytearray - the accepted answer is more Kotlin idiomatic and not as error prone as your solution.
m
Thanks let ill check.