Storing Android Emoji in MySQL

拟墨画扇 提交于 2020-01-03 05:29:09

问题


We are storing the emoji selected by the users in text body in MySQL. We have the column looks like this

`body` text CHARACTER SET utf8 COLLATE utf8_bin,

Original data : <div id='mobile-question-style' style=\"font-family: 'Helvetica Neue',Helvetica,Arial; color:#333333;\">\uD83D\uDE1D\uD83D\uDE3E\uD83D\uDE3E\uD83D\uDE3A\uD83D\uDE3A\uD83D\uDE1D</div>

Data in DB : <div id='mobile-question-style' style="font-family: 'Helvetica Neue',Helvetica,Arial; color:#333333;">????????????</div>

But when if fetch it from the Rest API it looks correct : "body":"<div id='mobile-question-style' style=\"font-family: 'Helvetica Neue',Helvetica,Arial; color:#333333;\">\uD83D\uDE1D\uD83D\uDE3E\uD83D\uDE3E\uD83D\uDE3A\uD83D\uDE3A\uD83D\uDE1D</div>",

Now when I update the question, using Rest API it looses it encoding and showes "????"

Data from endpoint after I update : "body":"<div id='mobile-question-style' style=\"font-family: 'Helvetica Neue',Helvetica,Arial; color:#333333;\">????????????</div>",

There is special logic when updating the body so does any one knows what is going on here?


回答1:


To store emojis into a Mysql Database convert the string you are storing into an encoded Base 64 string. On your application side all you have to do is decode that string using Base 64.

Encoding

                byte[] data = editTextFieldWithEmojis.getText().toString().getBytes("UTF-8");
                base64String = Base64.encodeToString(data, Base64.DEFAULT);

Decoding

                byte[] data = Base64.decode(stringWithEmojis, Base64.DEFAULT);
                newStringWithEmojis = new String(data, "UTF-8");


来源:https://stackoverflow.com/questions/24684739/storing-android-emoji-in-mysql

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!