2

In Android, Is there a way to display base64 image in the TextView with the text? I’m getting the base64 image from server in <img /> tag. It’s an HTML.

Please check below HTML:

img {
  vertical-align: middle;
}

<p>10 + <img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAIoAAABUCAIAAADS0ThsAAAEpElEQVR4nO2c24GtIAxFqSf9UA/9UA/9OB+KPAN4fLAds7/u1aMyWZCEKKhFBCw1uwGilgQPtAQPtAQPtAQPtAQPtAQPtAQPtAQPtAQPtAQPtAQPtKDwOGeNJlK7iEgb6364kyGllCLDXLudV0ppe67N9woFj7M6wpLrMKQOnkAHnA8EnmAt0sa6YFLnbBhN+gChNp71LOm1RyDzAcBj9YaGGyH7yGJ9VXlJC892UtueC5yv6Xj8yGn3Yf+rUUu2DL91B237IWq6ZuOJbNUWjzHNJ9Yw1bB78kR0PpPxDNPh+NQzCh9WKmbPnjg2eKdpLp5Dnbf88Z5S0J42OGciYOWNi/6AzWcungODp/Lr7f8sg8qpygOh+bwYT+NiJpNgSLCY5+u9eEZy5/QkO05w+bw39uyzl+qPV4snN45LBXXh8Xlv5nYUT58OYAACnve4dtw46Nw6GYDPJsD4zMbDms0beEuZK+ZrRYziXDe8YPKZjidKs5KaZ1wMJf+v1HbsgAjzIU9jIPiH0h9QAALAsywuTFSyinVSFCg79m5SbfxVySXe1EOp2dG63hOCwLMsy5LM9hlVXvvUA742SewZTJwB+cDgWZbFVzdjgxORNsbaCF5hvdgP1kqiw9OanTVMAMLC09T2bg6ob9+vF+H5ogQPtAQPtAQPtAQPtAQPtAQPtAQPtAQPtPp4eoUw0Y+6Bo9oogQPtAQPtAQPtF6LJ33JQ0cW/zwqq9WJ90fvxFN9R0owL9F2nf5A+I14wvIpf8BCrgO5YAHrG/FU/cU5J3KDnCGlyJgPOreKwPDszfli7CllNZJzi5gIHr/SGhDOLDy93pot+Lwx8QVbHro2J/CYgaf95Vj46DPV9ZEh+xB7vrZ8IGrN43jCV7Ktr8+jfQp83nttFwf8prO9SOWXhh7Ekz6/8kDOaBcvQAPzaF4T8UTflvPr0nmzXWnQlTVOFt3SQ85td/PG8bZ+pFO3uug9yPKNstSBjbIew6NDkzgMx5by/igm8bjpyY3OMFTkm5G5MXjiw0mPI9LW1Xrb0IJPVe0KA63MknuuEf0/NM0Ng5u/PfZdimcfPHWrN/aHuIFOvvjXWX34NqyzfmqvijvwbP0tuELLlG6dCfNVZyg9Wxz4oYXFvgaXmfMRN34bHna5Z3P7yOQqq8/89XfUeZxzzloTe8xX4hlbLF2cLmbbp6y7Boh+PcFFK+9qCVl8vtCr8DQ9cn//yGKTiNOdP1kHXG1VJfol+yEl54mItDbWulfGnqZHPobn/Osb795aw2dLH/b4Z7PdRcNmB8yK43fhabW6HUxzX3YWz1DorpTdk4Izf5OnNqm4Fg8fYTrdzRlKzp2N7EPli6oDjQ9yeJ7bBftqPNUdCvsV63ywnH1TMOR86iM0OhotpN9nT72dMK7V5XgWbn/PVg0kf4eVhuyfbFCGjbWI0HWgyVGmqkHmoW+D7sCzLOn+4P0KotXlC1WrG8WgsVbabMf/rBUDePK7hHs8s0Xfv/jW4EeN4ZmqL+Pppgbz9WU83cR6vj6Npzstna5v4+kWdWbr63jWWVmrJDpVggdaggdaggdaggdaggdaf09kF5esT+t9AAAAAElFTkSuQmCCAA==" width="102" height="68">+10</p>

If it’s not possible in TextView then what is the best way to display image without using ImageView(because I also need to display text with the image)?

Note: The image can be anywhere in the sentence.

Sanjay Kakadiya
  • 1,596
  • 1
  • 15
  • 32
  • I don't think I've ever seen someone try to load an Image in a TextView. Also, if you want to display text over your image then you can use a FrameLayout which includes an ImageView and TextView. To use a Base64 for an image check this https://stackoverflow.com/questions/4837110/how-to-convert-a-base64-string-into-a-bitmap-image-to-show-it-in-a-imageview – Zun Jun 21 '18 at 07:39
  • @ZUNJAE It's **very common** to use a TextView to display images and text. It's a single View, which performs much better than a FrameLayout. – Phantômaxx Jun 21 '18 at 07:50
  • 2
    well, in that case use setBackgroundDrawable, but considering I don't know what OP wants to display, an ImageView still makes the most sense to display an Image – Zun Jun 21 '18 at 07:52
  • @ZUNJAE I'm able to display image if I get image URL in `` but not able to display if there is base64 string. – Sanjay Kakadiya Jun 21 '18 at 08:34
  • Because an image ***`ENCODED`*** in Base64 is actually a **string**, not an **image**. If you want to display it, you'll have to first ***`DECODE`*** it back to an **image**. Then an ImageView is more than enough to show it properly. – Phantômaxx Jun 21 '18 at 11:54
  • @KlingKlang This will work only for Image. If I get image and text then it does not work because the `ImageView` will not display text. – Sanjay Kakadiya Jun 22 '18 at 06:27
  • A Base64 encoded image will return an **image** anyway. If the image represents some text, the text will be (obviously) shown when the image is reconstructed, as it is part of it (i.e.: it's **pixels**, not **vectors**). – Phantômaxx Jun 22 '18 at 07:21
  • @KlingKlang Please read the question aging. You will get the idea of what I want. – Sanjay Kakadiya Jun 22 '18 at 10:00
  • Please read my previous comment again. There is ***no actual text*** in a Base64 encoded **image**. – Phantômaxx Jun 22 '18 at 10:46
  • Did you find any solution to this? – ice spirit May 22 '21 at 08:56

1 Answers1

0

Get Bitmap from Base64 and convert it into Drawable and set to TextView

    byte[] decodedString = Base64.decode(encodedImage, Base64.DEFAULT);
    Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length); 
    Drawable d = new BitmapDrawable(getResources(), decodedByte);
    textView.setBackgroundDrawable(d);
Rutvik Bhatt
  • 3,185
  • 1
  • 16
  • 28
Milan Pansuriya
  • 2,521
  • 1
  • 19
  • 33