I am using Ionic 2 calling a RESTful Service (JSON) in Java and a MySQL database. I am trying to display an image that is stored in the database, and render it in ionic.
Everything works perfectly, except I am struggling to get an image to display.
I have a PNG image stored in MySQL (type LONGBLOB). Then I access it, and in Java, convert it to Base64.
import org.apache.commons.codec.binary.Base64;
subCategory.setIcon(Base64.encodeBase64(subCategory.getIcon()));
JSON:
"icon" : "Vm0wd2QyUXlVWGxWV0d4V1YwZDRWMVl3Wk...lpRVVQwOQ=="
Then once received via JSON, I display it in html:
<img src="data:image/png;base64,{{item.icon}}" />
It just displays the image placeholder with no image.
I have also tried to decode the image with this Javascript:
icon = atob(icon);
and
b64DecodeUnicode(str) {
return decodeURIComponent(Array.prototype.map.call(atob(str), function(c) {
return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2);
}).join(''));
}
with no success.
<img src="some url here" />and setting up an HTTP server that serves image data from MySQL? Aside: Base64 anddecodeURIComponentare entirely unrelated; one is not the inverse of the other.decodeURIComponent. The reason I have it stored in the database is so that I can add images by just updating the database. Second, I want to keep it simple, the reason why I don't serve them as http, is because I have RESTful JSON services, and am making a call to get rest of the data anyway, I don't want to make another http call.decodeURIComponentmakes no differece, i.e. the image still does not display.